Search code examples
perlsocketsbanner

I can't win the banner with Socket in Perl


I'm trying to connect to a server and earn the banner, but I'm not succeeding.

My script runs in theory as follows:

  1. Get port and IP but I can use port e IP static on variables.

  2. Execute the socket, with timeout 7

  3. If connected

  4. Wait the response e use the handle <> to get banner

  5. Execute print to show banner.

use IO::Socket::INET

$myip = <stdin>;     # $myip is the remote IP to connect
chomp($myip);
$myport = <stdin>;   # $myport is the remote port to connect 
chomp($myport);

# This part of data entry tends to use IP and URL frames, but it doesn't work either.
$sock=IO::Socket::INET‐>new("$myip:$myport");

if($sock){
    $remote=IO::Socket::INET‐>new(
        Proto=>"tcp",
        PeerAddr=>$myip,
        PeerPort=>$myport,
        Timeout=>"7"
    );
    $line=<$remote>;
    print $line;
}

When I run the code, the connection is executed, but I don't gain banner. I cannot find an error in this code. I looked for a solution on other websites, but I couldn't find it. It's always the same examples and they don't change.


Solution

  • http protocol expects a request from a client before it sends any reply.

    Try the following code snippet with ip address of your http server.

    #!/usr/bin/env perl
    #
    # vim: ai ts=4 sw=4
    
    use strict;
    use warnings;
    
    use IO::Socket::INET;
    
    my $ip   = '192.186.1.120';
    my $port = '80';
    
    my $sock = IO::Socket::INET->new("$ip:$port");
    
    print $sock "GET / HTTP/1.0\n\n";
    
    print while <$sock>;
    

    Reference: HTTP flow