SHARE
TWEET

PortScanner [PERL] |

a guest Oct 21st, 2015 808 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2. # Easy port scanner
  3. # ./scanner.pl
  4.  
  5. use Socket;
  6.  
  7. $| = 1; # so \r works right
  8.  
  9. my ($ip, $protocol, $port, $myhouse, $yourhouse, $log);
  10.  
  11. $protocol = getprotobyname('tcp');
  12.  
  13. ($ip, $port, $port_stop, $log) = @ARGV;
  14.  
  15. if ($ip eq "-h") {
  16.     &usage();
  17. }
  18.  
  19. $ip = "localhost" if not $ip;
  20. $port = 1 if not $port;
  21. $port_stop = 1024 if not $port_stop;
  22. $log = "qsopenports.txt" if not $log;
  23.  
  24. unless (open(LOG_FILE, ">>$log")) {
  25.     die "Can't open log file $log for writing: $!\n"
  26. }
  27.  
  28. # Make file handle hot so the buffer is flushed after every write
  29. select((select(LOG_FILE), $| = 1)[0]);
  30.  
  31. print LOG_FILE "The following ports are open on $ip between port $port and $port_stop\n\n";
  32.  
  33. print "Checking $ip for open ports..\n";
  34.  
  35. for (; $port < $port_stop; $port += 1) {
  36.     socket(SOCKET, PF_INET, SOCK_STREAM, $protocol);
  37.  
  38.     $yourhouse = inet_aton($ip);
  39.  
  40.     $myhouse = sockaddr_in($port, $yourhouse);
  41.  
  42.     if (!connect(SOCKET, $myhouse)) {
  43.         printf "%d\r", $port;
  44.     } else {
  45.         printf "%d  <- open\n", $port;
  46.         print LOG_FILE "$port\n";
  47.         close SOCKET || die "close: $!";
  48.     }
  49. }
  50.  
  51. close LOG_FILE || die "close: $!";
  52. printf "QuickScan complete.\n";
  53. printf "Those are the open ports for: $ip\n";
  54.  
  55. sub usage() {
  56.     print "Usage: ./quickscan [host] [start port] [stop port] [logfile]\n";
  57.     print "Defaults to localhost and port 1 and port 1024 qsopenports.txt\n";
  58.     exit 0;
  59. }
RAW Paste Data
Top