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
-
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.