Advertisement
Guest User

Untitled

a guest
Jun 17th, 2014
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.69 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3.     use Net::IP;
  4.     use Net::Ping;
  5.     use IO::Select;
  6.     use IO::Socket::INET;
  7.     use Term::ANSIColor;
  8.     use vars qw( $PROG );
  9.     ( $PROG = $0 ) =~ s/^.*[\/\\]//;
  10.     #Utilisation
  11.     if ( @ARGV == 0 ) {
  12.             print "Utilisation : perl $PROG.pl [Debut de la ranger] [Fin de la ranger] [Port] [Threads] [Delay] [Fichier.txt]\n";
  13.         exit;
  14.     }
  15.     my $threads  = $ARGV[3];
  16.     my @ip_team  = ();
  17.     $|= 1;
  18.     my $ip   = new Net::IP ("$ARGV[0] - $ARGV[1]") or die "Erreur ranger d adresse IP Invalide.". Net::IP::Error() ."\n";
  19.  
  20.  
  21.     #on fork ^^
  22.     while ($ip) {
  23.     push @ip_team, $ip++ ->ip();
  24.     if ( $threads == @ip_team ) { Scan(@ip_team); @ip_team = () }
  25.     }
  26.     Scan(@ip_team);
  27.  
  28.     #Scan
  29.     sub Scan
  30.     {
  31.     my @Pids;
  32.  
  33.             foreach my $ip (@_)
  34.             {
  35.             my $pid        = fork();
  36.             die "Fork impossible! $!\n" unless defined $pid;
  37.  
  38.                     if  (0 == $pid)
  39.                     {
  40.                                     alarm 1;
  41.  
  42.  
  43.                                     # temps d'attente du  "destination unreachable" packet
  44. my $icmp_timeout=2;
  45. # creation d'un icmp socket pour  "destination unreachable" packets
  46. $icmp_sock = new IO::Socket::INET(Proto=>"icmp");
  47. $read_set = new IO::Select();
  48. $read_set->add($icmp_sock);
  49.  
  50. # le buffer a envoyer en UDP
  51. my $buf="hello";
  52.  
  53.  
  54.  
  55.  
  56.       # crée un socket udp pour l'ip et le port
  57.     my $sock = new IO::Socket::INET(PeerAddr=>$ip,
  58.     PeerPort=>$ARGV[2],
  59.     Proto=>"udp",
  60.     Timeout => $ARGV[4]);
  61.      #on envoye le buffer et ferme le socket
  62.     $sock->send("$buf");
  63.     close($sock);
  64.  
  65.       # attente de recevoir les packets
  66.     ($new_readable) = IO::Select->select($read_set, undef, undef, $icmp_timeout);
  67.       # arrivé des flags
  68.     $icmp_arrived = 0;
  69.       # pour tout les socket on a recu un packet (seulement - icmp_socket)
  70.     foreach $socket (@$new_readable)
  71.     {
  72.            # si nous avons reussi ce qui est probable nosu avons  "destination unreachable"
  73.          if ($socket == $icmp_sock)
  74.          {
  75.                 # met le flags et nettoye le buffer et sockets
  76.               $icmp_arrived = 1;
  77.               $icmp_sock->recv($buffer,50,0);
  78.          }
  79.     }
  80.  
  81.  
  82.  
  83.  
  84.  
  85.                                     open (MYFILE, ">>$ARGV[5]");
  86.                             if ( $icmp_arrived == 0 ) {
  87.                     print MYFILE "$ip\n";
  88.                      print "IP TROUVER  :  $ip\n";
  89.                     close (MYFILE);}
  90.                     exit
  91.                     }
  92.                     else
  93.                     {
  94.                     push @Pids, $pid
  95.                     }
  96.             }
  97.  
  98.     foreach my $pid (@Pids) { waitpid($pid, 0) }
  99.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement