Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Problem in IO::Socket syntax? by c

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-24 10:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found