http://qs1969.pair.com?node_id=155509

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

I've written a short script that checks to see if a remote server is listening on a specific TCP port. Its in the early stages, so some error checking needs to be built in. For now though, I'm confused by an error that I am getting when I test it using localhost as the guinea pig.

1 #!/usr/bin/perl -w 2 3 # $Id:$ 4 5 # check to see if server is listening on a tcp port 6 7 use strict; 8 use IO::Socket; 9 10 my @servers = qw(localhost); 11 12 my %ports = ( 13 telnet => "23", 14 smtp => "25", 15 pop3 => "110", 16 named => "53", 17 http => "80", 18 https => "443", 19 ); 20 21 my $proto = "tcp"; 22 my $disco = 0; 23 24 for my $host(@servers) { 25 26 my $ip = gethostbyname($host); 27 my $ip_addr = inet_ntoa($ip); 28 29 for my $service(keys %ports) { 30 31 my $disco = 0; 32 33 my $checkport = IO::Socket::INET->new( 34 PeerAddr => "$ip_addr", 35 PeerPort => "$ports{$service}", 36 Proto => "$proto", 37 Timeout => '0') 38 or $disco = "1"; 39 40 print "$host FAILED to repond on $service $ports{service}\ +n" 41 if $disco; 42 43 close $checkport; 44 45 } 46 }

When I run the script, I get:

Use of uninitialized value in concatenation (.) at ./chkport.pl line 4 +0. localhost FAILED to repond on telnet Can't use an undefined value as a symbol reference at ./chkport.pl lin +e 43.

i'm staring at the print statement and the close statement. Both seem to be correct to the best that I can tell. Your input is well appreciated as always.

humbly -c