1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
#!/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) }
} |
Partager