Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Ping and check for server running on port 27015

by Gibble (Novice)
on Jan 23, 2001 at 21:13 UTC ( [id://53728]=perlquestion: print w/replies, xml ) Need Help??

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

I need to write a simple perl script that will ping an IP and check to see if there is a server running on port 27015. (It is for a website so we can show wether the server is active or not).
  • Comment on Ping and check for server running on port 27015

Replies are listed 'Best First'.
Re: Ping and check for server running on port 27015
by andye (Curate) on Jan 23, 2001 at 22:20 UTC
    Consider the situation where the webserver has crashed, but the underlying machine is running perfectly OK.

    You might prefer to generate a special page with a particular value (e.g. 'success') which you can then retrieve, and if it comes back ok, then Everything Must Be All Right(tm).

    Something like

    use LWP::Simple; print "Broken!\n" unless get('http://your.site/page') eq 'success';

    Alternatively, you could go to the Uptime tool at arsdigita.com, where someone else has done the work for you!

    andy.

Re: Ping and check for server running on port 27015
by c-era (Curate) on Jan 23, 2001 at 21:22 UTC
    You will want to check out the Net::Ping module, it will be very useful.
      I looked at the module and it shows nothing about ports
        use IO::Socket then...
        Although you must be warned that ping isn't a PERFECT way to check if a server is up as some boxes are configured to ignore ICMP echo, and some routers even drop those packet...
        (try pinging www.microsoft.com for example)
        IMHO a tcp connect on a well known USED port (port 80 in the microsoft case...) is far better.

        In brief if you use IO::Socket, Net::Ping shouldn't be necessary...
        For pinging a particular TCP port you'll want to use something like IO::Socket::INET to make a connection to that port. The TCP option of Net::Ping has no port option and will only connect to the echo service/port.
Re: Ping and check for server running on port 27015
by mr.nick (Chaplain) on Jan 23, 2001 at 22:26 UTC
    Hm. Now I am confused (and/or stupid; your choice). I was thinking that perhaps Net::Ping would support which port to perform a "tcp ping" against (after all, there is a hash element called "port_num") in the initialization of the class:
    elsif ($self->{"proto"} eq "tcp") { $self->{"proto_num"} = (getprotobyname('tcp'))[2] || croak("Can't get tcp protocol by name"); $self->{"port_num"} = (getservbyname('echo', 'tcp'))[2] || croak("Can't get tcp echo port by name"); $self->{"fh"} = FileHandle->new(); }
    So I figured I'd create the class and modify $self->{"port_num"} by myself. Well, I've run into a problem: the following code doesn't work:
    #!/usr/bin/perl use Net::Ping; $host="localhost"; $p = Net::Ping->new(); print "$host is alive.\n" if $p->ping($host); $p->close();
    It produces the error message of
    Bad arg length for Socket::unpack_sockaddr_in, length is 0, should be +16 at /usr/lib/perl5/5.00503/i386-linux/Socket.pm line 295.
    Now, I realize that I'm probably missing something here, but I can't figure out what it is; after all, this example is the first listed in the pod for Net::Ping.

    Any ideas?

      I believe that Net::Ping isn't checking whether the hostname-to-IP-address lookup failed and is then trying to ping to an undefined IP address. You could try updating the module.

              - tye (but my friends call me "Tye")
Re: Ping and check for server running on port 27015
by Gibble (Novice) on Jan 23, 2001 at 22:04 UTC
    Ok it looks like I have it figured out now tx all
Re: Ping and check for server running on port 27015
by Gibble (Novice) on Jan 24, 2001 at 00:43 UTC
    On another help site I frequent someone suggested using something like the following to ping the server and port.
    #!/usr/local/bin/perl use IO::Socket; ## Array of ports that you want to check. $port = "27015"; $remoteip = "www.your_server.com"; print "\nChecking for service at $remoteip\n"; print " "; print "=" x (length($remoteip) + 25); print "\n\n"; $connected = 0; $checkport = IO::Socket::INET->new( PeerAddr => "$remoteip", PeerPort => "$port", Proto => 'tcp', Timeout => '0') or $connected = 1; if (!($connected)) { print " Port $port is up.\n"; } else { print " Port $port is down.\n"; } close $checkport;
    What do you think, will this work?
      While i haven't a comment on the socket side of your code, you may want to reverse the logic of your $connected flag. Intuitively, if I see a flag such as "$connected", the assumption is that a true value would mean we are conencted.

      Then you can say something like:

      if ( $connected) { print " Port $port is up.\n"; } else { print " Port $port is down.\n"; }
      That also means you will have to reverse the logic in your "or" modifier, but your program will more intuitive and easier to maintain.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://53728]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found