scanner_port.perl
· 1.4 KiB · Text
Raw
#!/usr/bin/perl
# Easy port scanner
# I wrote this in the 90s to help learn socket programming
# ./quickscan -h for usage
use Socket;
$| = 1; # so \r works right
my ($ip, $protocol, $port, $myhouse, $yourhouse, $log);
$protocol = getprotobyname('tcp');
($ip, $port, $port_stop, $log) = @ARGV;
if ($ip eq "-h") {
&usage();
}
$ip = "localhost" if not $ip;
$port = 1 if not $port;
$port_stop = 1024 if not $port_stop;
$log = "qsopenports.txt" if not $log;
unless (open(LOG_FILE, ">>$log")) {
die "Can't open log file $log for writing: $!\n"
}
# Make file handle hot so the buffer is flushed after every write
select((select(LOG_FILE), $| = 1)[0]);
print LOG_FILE "The following ports are open on $ip between port $port and $port_stop\n\n";
print "Checking $ip for open ports..\n";
for (; $port < $port_stop; $port += 1) {
socket(SOCKET, PF_INET, SOCK_STREAM, $protocol);
$yourhouse = inet_aton($ip);
$myhouse = sockaddr_in($port, $yourhouse);
if (!connect(SOCKET, $myhouse)) {
printf "%d\r", $port;
} else {
printf "%d <- open\n", $port;
print LOG_FILE "$port\n";
close SOCKET || die "close: $!";
}
}
close LOG_FILE || die "close: $!";
printf "QuickScan complete.\n";
printf "Those are the open ports for: $ip\n";
sub usage() {
print "Usage: ./quickscan [host] [start port] [stop port] [logfile]\n";
print "Defaults to localhost and port 1 and port 1024 qsopenports.txt\n";
exit 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 | 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 | } |