common has asked for the wisdom of the Perl Monks concerning the following question:

i need a way to represent an ip when scanning a c-range for web server info.
use IO::Socket; my $first_var = "64.219.21.1"; my $last_var = "25"; my ($net,$first) = ($first_var =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3})\.(\d +{1,3})$/ ); die "undetermined network part in $first_var.\n" unless $net; die "undetermined host part in $first_var.\n" unless $first; my ($last) = ($last_var =~ /^(\d{1,3})$/ ); die "undetermined host part in $last_var.\n" unless $last; ($last, $first) = ($first, $last) if $last < $first; my @hosts = ($first..$last); for ($li=0;$li<=$#hosts;$li++){ &scan() }
if scan() contains print "$net.$hosts[$li]"; it executes perfectly, printing out all the ip's with no newlines or anything. but let's say this is scan:
sub scan { $host = "$net.$hosts[$li]"; $socket = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $host, PeerPort => 80) or die $!; $socket->autoflush(1); print $socket "HEAD / HTTP/1.0\015\012\015\012"; while (<$socket>) { print; last if /^$/; } close($socket); }
this gives an unknown error. i tried replacing $host with the url "feartech.net" it performed perfectly. so i suppose my question is, how to express the ip in a manner that the connection will go through. thanks for your help.

Replies are listed 'Best First'.
Re: socket problems
by Aristotle (Chancellor) on Oct 17, 2002 at 01:26 UTC

    "An unknown error" isn't good enough. We have no idea what's going wrong from your description.

    Please use stric&#359; and warnings. You're using a subroutine that takes a parameter, but are passing it implicitly using globals - that will get you in trouble sooner or later.

    Also, you should almost never use the for(;;) loop form; use foreach(LIST) instead, it avoids so called "fence post" errors (where you do one iteration too few or too many). You're almost doing that with your @host variable after all..

    Lastly, your parameter passing format - initial IP and terminating last octet - feels pretty awkward (for no reason I could immediately pin down).

    And choose better variable names than $first_var and $last_var..

    Something like this:

    #!/usr/bin/perl -w use strict; use IO::Socket; sub scan { my ($addr) = @_; $socket = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $addr, PeerPort => 80 ) or die $!; $socket->autoflush(1); print $socket "HEAD / HTTP/1.0\015\012\015\012"; print until ($_ = <$socket>) =~ /^$/; close($socket); } my $network = "64.219.21" my @addr = (1, 25); @addr[0,1] = @addr[1,0] if $addr[1] < $addr[0]; scan "$network.$_" for $addr[0] .. $addr[1];
    (Untested.)

    Makeshifts last the longest.

Re: socket problems
by common (Acolyte) on Oct 20, 2002 at 22:21 UTC
    unfortunetly active state gave an unknown error when it couldn't connect to the http port of inactive hosts. you can see my confusion then. i believed it was with the way i was inputting my ip. i also corrected my code in many ways thanks to your post, i went for a foreach loop and some other things. thanks.