Never been to DZone Snippets before?

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

About this user

Sylvain Le Courtois

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

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
   1  
   2  use strict;
   3  use Net::Ping;
   4  use IO::Socket::INET;
   5  
   6  sub isodate() {
   7          my ($day, $mon, $year, $hour, $min, $sec) = (localtime)[3, 4, 5, 2, 1, 0];
   8          $mon++; # 0-based index
   9          $year = $year + 1900;
  10          my $date = sprintf ("%04i-%02i-%02i %02i\:%02i\:%02i", $year, $mon, $day, $hour, $min, $sec);
  11          return $date;
  12  }
  13  
  14  sub testhost {
  15         my $p = new Net::Ping("tcp");
  16         $p->{port_num}=80; 
  17         my @result = $p -> ping($_[0],2);
  18         return $result[0];
  19         }
  20  
  21  sub to_dot {
  22  	my $n = shift;
  23  	my @decimal;
  24  	for (1..4) {
  25  		unshift @decimal, $n & 0xFF;
  26  		$n >>= 8;
  27  	}
  28  	return join(".",@decimal);
  29  }
  30  
  31  my %dejavu;
  32  open FH,"liste.txt";
  33  while (<FH>) {
  34  	chomp;
  35  	my ($routeur,$mask)=split;
  36  	
  37  	next if $routeur !~ /\d+\.\d+\.\d+\.\d+$/ or $mask !~ /\d+\.\d+\.\d+\.\d+$/;
  38  	
  39  	next if defined($dejavu{$routeur});
  40  	$dejavu{$routeur}=1;
  41  	
  42  	my ($o1,$o2,$o3,$o4) = split /\./,$mask;
  43  	my $mask=$o1*256**3+$o2*256**2+$o3*256+$o4;
  44  	my $num = $mask ^ 0xFFFFFFFF;
  45  	$num--;
  46  
  47  	my ($o1,$o2,$o3,$o4) = split /\./,$routeur;
  48  	my $net=$o1*256**3+$o2*256**2+$o3*256+$o4 & $mask;
  49  	
  50  	#print join("|",$routeur,&to_dot($net),$num)."\n";
  51  	
  52  	print "Starting scanning network ".to_dot($net).", router = ".$routeur."\n";
  53  	print "Adresses demarrant de ".to_dot($net+1)." et finissant a ".to_dot($net+$num).".\n";
  54  	for my $i (1..$num) {
  55  		my $host=to_dot($net+$i);
  56  		if ( &testhost($host) ) {
  57  			print "$host is alive\n";
  58  			my $port=80;
  59  			my $sock = new IO::Socket::INET (PeerAddr => $host,
  60  					     PeerPort => $port,
  61  					     Proto => 'tcp');
  62  			if ($sock){
  63  				close $sock;
  64  				print "$port -open on $host\n";
  65  				open OUT,">>webservers.txt";
  66  				print OUT join("|",isodate(),$host,to_dot($net),$routeur)."\n";
  67  				close OUT;
  68  			}	else	{
  69  				print "$port -closed on $host\n";
  70  			}
  71  
  72  		} else {
  73  			print "$host is not responding\n";
  74  		}
  75  	}
  76  }
  77  
  78  
  79  close FH;
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS