#!/usr/bin/perl -w ### # scan a range of ports on a given machine # see Cookbook, pg 604, chap 17 # # see Net::Telnet (chap 18, Cookbook), for a telnet read/write by # port interface. # # also see /etc/inet/services (or /etc/services) # ##### use IO::Socket; $remote_host = "big.marlboro.edu"; $start_port=0; $stop_port=512; ####### # One approach is to # find all the ports listed in /etc/inet/protocols. # each line is # protocol port/type ... # where type is "tcp" or "udp" # # I'm doing something simpler right now, namely # just trying all ports from 0 to 512 (arbitrarily, as an example). ######## @open=(); @skipped=(); for ($remote_port=$start_port; $remote_port<$stop_port+1; $remote_port++) { # current version gets stuck on these (Timeout doesn't seem to fix?) my $problems = ",9,23,111,512,"; push(@skipped, $remote_port), next if $problems =~ m/,$remote_port,/; # 9 - ? # 23 - telnet/tcp # 111 - sunrpc/udp # 512 - ? print "---",$remote_port,"---:\n"; $socket = IO::Socket::INET->new(PeerAddr => $remote_host, PeerPort => $remote_port, Type => SOCK_STREAM, Timeout => 1, ); #### don't want to report lots of errors; many will fail. # or die "Couldn't connect to $remote_host:$remote_port : $@\n"; if (defined $socket) { print "opened port $remote_port : "; push @open, $remote_port; # ... do something with the socket print $socket "test foo \n\n\n"; $answer = <$socket>; if (defined $answer) { print "returned '$answer' ; \n"; } else { print "nothing returned \n"; } # and terminate the connection when we're done close($socket); } } print "========\n"; print " ports that tested open from $start_port->$stop_port : @open \n"; print " ports skipped: @skipped \n";