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

verifying if a server is listening on a port

Thread Next
January 16, 2002 13:00
verifying if a server is listening on a port
Message ID:
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 = "";
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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About