Last active 1748862147

Revision 97ab37054bdbfad231bfa48a90343dfaf60f5cf7

scanner_port.perl Raw
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
5use Socket;
6$| = 1; # so \r works right
7my ($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;
17unless (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
21select((select(LOG_FILE), $| = 1)[0]);
22print LOG_FILE "The following ports are open on $ip between port $port and $port_stop\n\n";
23print "Checking $ip for open ports..\n";
24for (; $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}
36close LOG_FILE || die "close: $!";
37printf "QuickScan complete.\n";
38printf "Those are the open ports for: $ip\n";
39sub 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}