rdkeith830 has asked for the wisdom of the Perl Monks concerning the following question:

Following is a PERL script that reads an input file of IP's and port ranges. I then checks each port to see if they are open. It works as written. The problem I have is I want to know if the port is OPEN (Lisntened) Closed (no app has it open) Blocked (the firewall will not allow me to open it) Adjusting the attached code is there a way to better check the PORT state so I can recognize all three states? I am not an experienced PERL coder but been coding lots of different languages for years. I just don't know what to check to get the port state. Thanks for any help you can offer.
#!/usr/local/bin/perl # #################################################################### # SINGLE THREAD PERL PORT SCANNER # Modified from FORK PERL PORT SCANNER By Jonathan Worthington # This script is made available under the sames terms as Perl itself. # #################################################################### + use strict; use warnings; use IO::Socket::INET; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(t +ime); $year += 1900; print "Port Scan for IRN $mon/$mday/$year $hour:$min:$sec \n"; my $iFile = $ARGV[0]; open(INPUT, '<', $iFile) or die $!; while (<INPUT>) { my $hostIp = substr($_, 0, 14); my $hostName = substr($_, 18, 7); my $portStart = substr($_, 26, 5); my $portTimes = substr($_, 32, 5); #Auto-flush. $| = 1; #Port scan host. print "Scanning $hostName, $hostIp, Port $portStart for $portTimes num +ber of Ports \n"; my $port; my $endPort = $portStart+$portTimes - 1; for ($port=$portStart; $port<=$endPort; $port++) { #Attempt to connect to $host on $port. my $socket; my $success = eval { #local $SIG{'ALRM'} = sub { die "Timed out" }; #alarm(1); $socket = IO::Socket::INET->new( PeerAddr => $hostIp, PeerPort => $port, Proto => 'tcp', Timeout => "1" ) }; #If the port was opened, say it was and close it. if ($success) { print "Host $hostName Port $port: Open\n"; shutdown($socket, 2); } else { print "Host $hostName Port $port: NOT Open\n"; } } } close INPUT; print " \n"; print "*************************************************** \n"; print "Port Scan for IRN is COMPLETE on this segment \n"; print "*************************************************** \n";

Replies are listed 'Best First'.
Re: check blocked ports
by anonymized user 468275 (Curate) on May 05, 2011 at 15:01 UTC
    One approach is to goto nmap.org, download their port scanner and then use CPAN module Nmap::Scanner to retrieve the status into Perl.

    One world, one people

      Sir, So I did download nmap. It is a little complicated for what I need. All I need is a way to enter IP:Port and get a status back of that port. I couldn't get nmap to do that for me. I am sure it is just user incompetence. I would have to think trying to open a socket gives more info than open or closed. If you know the syntax for nmap to enter "nmap ip port" (or range of ports ) and it returns a port state could you share that with me? I will keep searching.. Thank you so much for your help.
        nmap_scanner or Nmap::Scanner (same animal) is also available with more details than CPAN offers at SourceForge http://nmap-scanner.sourceforge.net/. It has loads of subspaces including Nmap::Scanner::Port if you want to focus on port and state. Nmap::Scanner::Host can get the ip port info you require. Check the documentation link from the above sourceforge page - you may need to use multiple subspaces of nmap_scanner but it really does have what you need in there! If you need coding help, please show us in code terms where you are with this so we know what the coding problems you are having are - even pseudocode is better than nocode!

        One world, one people