in reply to Testing for port connectivity

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 :)

Replies are listed 'Best First'.
Re^2: Testing for port connectivity
by Anonymous Monk on Sep 04, 2006 at 16:43 UTC
    Thanks McDarren You really helped me out much appreciated!!! Works like a champ!