DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Perl : Scan A List Of Networks, Looking For Hosts Responding On The Port 80 (http)
// Input : a list of address of routers, in dotted decimal notation
use strict;
use Net::Ping;
use IO::Socket::INET;
sub isodate() {
my ($day, $mon, $year, $hour, $min, $sec) = (localtime)[3, 4, 5, 2, 1, 0];
$mon++; # 0-based index
$year = $year + 1900;
my $date = sprintf ("%04i-%02i-%02i %02i\:%02i\:%02i", $year, $mon, $day, $hour, $min, $sec);
return $date;
}
sub testhost {
my $p = new Net::Ping("tcp");
$p->{port_num}=80;
my @result = $p -> ping($_[0],2);
return $result[0];
}
sub to_dot {
my $n = shift;
my @decimal;
for (1..4) {
unshift @decimal, $n & 0xFF;
$n >>= 8;
}
return join(".",@decimal);
}
my %dejavu;
open FH,"liste.txt";
while (<FH>) {
chomp;
my ($routeur,$mask)=split;
next if $routeur !~ /\d+\.\d+\.\d+\.\d+$/ or $mask !~ /\d+\.\d+\.\d+\.\d+$/;
next if defined($dejavu{$routeur});
$dejavu{$routeur}=1;
my ($o1,$o2,$o3,$o4) = split /\./,$mask;
my $mask=$o1*256**3+$o2*256**2+$o3*256+$o4;
my $num = $mask ^ 0xFFFFFFFF;
$num--;
my ($o1,$o2,$o3,$o4) = split /\./,$routeur;
my $net=$o1*256**3+$o2*256**2+$o3*256+$o4 & $mask;
#print join("|",$routeur,&to_dot($net),$num)."\n";
print "Starting scanning network ".to_dot($net).", router = ".$routeur."\n";
print "Adresses demarrant de ".to_dot($net+1)." et finissant a ".to_dot($net+$num).".\n";
for my $i (1..$num) {
my $host=to_dot($net+$i);
if ( &testhost($host) ) {
print "$host is alive\n";
my $port=80;
my $sock = new IO::Socket::INET (PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp');
if ($sock){
close $sock;
print "$port -open on $host\n";
open OUT,">>webservers.txt";
print OUT join("|",isodate(),$host,to_dot($net),$routeur)."\n";
close OUT;
} else {
print "$port -closed on $host\n";
}
} else {
print "$host is not responding\n";
}
}
}
close FH;




