Guest User

Untitled

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