develooper Front page | perl.beginners | Postings from January 2002

verifying if a server is listening on a port

Thread Next
From:
charles
Date:
January 16, 2002 13:00
Subject:
verifying if a server is listening on a port
Message ID:
Pine.LNX.4.33.0201161433450.19922-100000@ns1.lunarmedia.net
i am hoping to use perl to verify whether a remote machine is listening on 
UDP/69. i've found some examples, and modified them slightly to come up 
with the attached snippet of code. the output always seems to be that the 
"port is up". i am imagining that $disconn is never being set to true 
since the $checkport will always at least attempt a connection.

maybe my reasoning to that is incorrect, but i guess, that is why i am 
writing :) any help is appreciated. thanks -charles


#!/usr/bin/perl -w

use strict;
use IO::Socket;

my $remoteip = "10.6.21.10";
my $port = "69";
my $proto = "udp";
 my $disconn = 0;
 my $checkport = IO::Socket::INET->new(
              PeerAddr => "$remoteip",
              PeerPort => "$port",
              Proto => "$proto",
              Timeout => '0') or $disconn = 1;

 if ($disconn) {
   print "  Port $port is down.\n";
 } else {
  print "  Port $port is up.\n";
 }
 close $checkport;



Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About