Erm, you pretty much answered your own question (although I'm not quite sure where you cooked up the system("telnet $_") from).
open HOSTS, '<', 'hostnames.txt' or die "Cant open hostnames.txt\n"; while (my $host = <HOSTS>) { chomp($host); check_ports($host,$timeout,\%porthash); # Do other processing stuff here }

Cheers,
Darren :)

I strongly suspect that you are going to ask next how to use IO::Socket::PortState, given that you appear to have simply copy/pasted the example code from the documentation - but I may be wrong :)

Update: Okay, since somebody suggested that my initial response to your question may have been a bit mean (and because deep down I really am a very nice guy™), here is some tested and working code that gives the output you are looking for:

#!/usr/bin/perl -w use strict; use IO::Socket::PortState qw(check_ports); my $hostfile = 'hosts.txt'; my %port_hash = ( tcp => { 22 => {}, 443 => {}, 80 => {}, 53 => {}, 30032 => {}, 13720 => {}, 13782 => {}, } ); my $timeout = 5; open HOSTS, '<', $hostfile or die "Cannot open $hostfile:$!\n"; while (my $host = <HOSTS>) { chomp($host); my $host_hr = check_ports($host,$timeout,\%port_hash); print "Host - $host\n"; for my $port (sort {$a <=> $b} keys %{$host_hr->{tcp}}) { my $yesno = $host_hr->{tcp}{$port}{open} ? "yes" : "no"; print "$port - $yesno\n"; } print "\n"; } close HOSTS;

The above gives the following output:

Host - 1.2.3.4 22 - no 53 - no 80 - yes 443 - yes 13720 - no 13782 - no 30032 - no Host - 5.6.7.8 22 - yes 53 - no 80 - yes 443 - yes 13720 - no 13782 - no 30032 - no

Note that I changed a few of the port numbers to those that I knew would be open on the hosts I tested against, and (obviously) altered the ouput to protect the innocent :)


In reply to Re: Testing for port connectivity by McDarren
in thread Testing for port connectivity by Anonymous Monk

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.