nithir revised this gist . Go to revision
No changes
nithir revised this gist . Go to revision
1 file changed, 0 insertions, 0 deletions
scanner_port.perl renamed to scanner_port.pl
File renamed without changes
nithir revised this gist . Go to revision
1 file changed, 43 insertions
scanner_port.perl(file created)
| @@ -0,0 +1,43 @@ | |||
| 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 | + | use Socket; | |
| 6 | + | $| = 1; # so \r works right | |
| 7 | + | my ($ip, $protocol, $port, $myhouse, $yourhouse, $log); | |
| 8 | + | $protocol = getprotobyname('tcp'); | |
| 9 | + | ($ip, $port, $port_stop, $log) = @ARGV; | |
| 10 | + | if ($ip eq "-h") { | |
| 11 | + | &usage(); | |
| 12 | + | } | |
| 13 | + | $ip = "localhost" if not $ip; | |
| 14 | + | $port = 1 if not $port; | |
| 15 | + | $port_stop = 1024 if not $port_stop; | |
| 16 | + | $log = "qsopenports.txt" if not $log; | |
| 17 | + | unless (open(LOG_FILE, ">>$log")) { | |
| 18 | + | die "Can't open log file $log for writing: $!\n" | |
| 19 | + | } | |
| 20 | + | # Make file handle hot so the buffer is flushed after every write | |
| 21 | + | select((select(LOG_FILE), $| = 1)[0]); | |
| 22 | + | print LOG_FILE "The following ports are open on $ip between port $port and $port_stop\n\n"; | |
| 23 | + | print "Checking $ip for open ports..\n"; | |
| 24 | + | for (; $port < $port_stop; $port += 1) { | |
| 25 | + | socket(SOCKET, PF_INET, SOCK_STREAM, $protocol); | |
| 26 | + | $yourhouse = inet_aton($ip); | |
| 27 | + | $myhouse = sockaddr_in($port, $yourhouse); | |
| 28 | + | if (!connect(SOCKET, $myhouse)) { | |
| 29 | + | printf "%d\r", $port; | |
| 30 | + | } else { | |
| 31 | + | printf "%d <- open\n", $port; | |
| 32 | + | print LOG_FILE "$port\n"; | |
| 33 | + | close SOCKET || die "close: $!"; | |
| 34 | + | } | |
| 35 | + | } | |
| 36 | + | close LOG_FILE || die "close: $!"; | |
| 37 | + | printf "QuickScan complete.\n"; | |
| 38 | + | printf "Those are the open ports for: $ip\n"; | |
| 39 | + | sub usage() { | |
| 40 | + | print "Usage: ./quickscan [host] [start port] [stop port] [logfile]\n"; | |
| 41 | + | print "Defaults to localhost and port 1 and port 1024 qsopenports.txt\n"; | |
| 42 | + | exit 0; | |
| 43 | + | } | |