#!/usr/bin/perl use Net::IP; use Net::Ping; use IO::Select; use IO::Socket::INET; use Term::ANSIColor; use vars qw( $PROG ); ( $PROG = $0 ) =~ s/^.*[\/\\]//; #Utilisation if ( @ARGV == 0 ) { print "Utilisation : perl $PROG.pl [Debut de la ranger] [Fin de la ranger] [Port] [Threads] [Delay] [Fichier.txt]\n"; exit; } my $threads = $ARGV[3]; my @ip_team = (); $|= 1; my $ip = new Net::IP ("$ARGV[0] - $ARGV[1]") or die "Erreur ranger d adresse IP Invalide.". Net::IP::Error() ."\n"; #on fork ^^ while ($ip) { push @ip_team, $ip++ ->ip(); if ( $threads == @ip_team ) { Scan(@ip_team); @ip_team = () } } Scan(@ip_team); #Scan sub Scan { my @Pids; foreach my $ip (@_) { my $pid = fork(); die "Fork impossible! $!\n" unless defined $pid; if (0 == $pid) { alarm 1; # temps d'attente du "destination unreachable" packet my $icmp_timeout=2; # creation d'un icmp socket pour "destination unreachable" packets $icmp_sock = new IO::Socket::INET(Proto=>"icmp"); $read_set = new IO::Select(); $read_set->add($icmp_sock); # le buffer a envoyer en UDP my $buf="hello"; # crée un socket udp pour l'ip et le port my $sock = new IO::Socket::INET(PeerAddr=>$ip, PeerPort=>$ARGV[2], Proto=>"udp", Timeout => $ARGV[4]); #on envoye le buffer et ferme le socket $sock->send("$buf"); close($sock); # attente de recevoir les packets ($new_readable) = IO::Select->select($read_set, undef, undef, $icmp_timeout); # arrivé des flags $icmp_arrived = 0; # pour tout les socket on a recu un packet (seulement - icmp_socket) foreach $socket (@$new_readable) { # si nous avons reussi ce qui est probable nosu avons "destination unreachable" if ($socket == $icmp_sock) { # met le flags et nettoye le buffer et sockets $icmp_arrived = 1; $icmp_sock->recv($buffer,50,0); } } open (MYFILE, ">>$ARGV[5]"); if ( $icmp_arrived == 0 ) { print MYFILE "$ip\n"; print "IP TROUVER : $ip\n"; close (MYFILE);} exit } else { push @Pids, $pid } } foreach my $pid (@Pids) { waitpid($pid, 0) } }