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";

In reply to check blocked ports by rdkeith830

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.