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

Hi Monks, I need a script that will ping a server at specified intervals, and if it recieves no reply, will then send an e-mail. I can do the e-mail bit (i'm a beginner) but i have no idea how to tackle the pinging bit. This is going to run on a linux server and any snippets of code would be greatly appreciated. regards james

Replies are listed 'Best First'.
Re: Pinging.
by valdez (Monsignor) on Aug 07, 2002 at 11:08 UTC

    Try with Net::Ping, here is some code from the man page:

    use Net::Ping; $p = Net::Ping->new(); print "$host is alive.\n" if $p->ping($host); $p->close();

    Ciao, Valerio

      You can use AdminMisc's GetHostAddress($DNS_Name) this will supply you with the IP address.

      I would keep all the servers host names in a text file list. Then I read the complete list into an array like this

      open(srv,"c:/server.txt")|| die "$^E\n"; chomp ( my @servers =<srv>);
      then foreach item in the list get the Ip address.
      Foreach my $srv (sort @servers){ my $ip = Win32::AdminMisc::GetHostAddress($srv); print "\nhost: $srv\tIp Address: $ip\n";}
      Cheers,...Blackadder
        hi there. is this script for win32? will this script do the required as described above? what i realy think i need it to do is telnet my servers ip address on port 25 and if it recieves a response (i.e it doesn't time out) then all is ok, else display an error message
      sorry formy stupidity, but how would you add a 'host is dead' print to this script? thanks
        How would you add a 'host is dead' print to this script?

        Hi Anonymous Monk.
        #!/usr/bin/perl -w use strict; use Net::Ping; my $host; print "Host to ping? "; chomp( $host = <STDIN> ); my $p = Net::Ping->new(); if( $p->ping($host) ) { print "$host is alive!\n"; } else { print "$host appears to be dead.\n"; } $p->close();

        Hope this helps,
        -Katie.
        aaarrrgghh, now i have a real problem. I forgot that the firewall between my server and the outside world drops ping requests. is there any way to check to see if my server is alive using a TCP connect method to see if it recieves a response form a port? i.e connect to port 25 and see if my smtp server responds? thanks
Re: Pinging.
by Jaap (Curate) on Aug 07, 2002 at 15:24 UTC
    Ping doesn't use a port. I would not expect to have the monks almost read the script for you - just appreciate when they give you a good hint. Please don't abuse it. ---- To me, boxing is lake ballet. Except there's no music, no choreography and the dancers hit each other.
      That's like saying, Sharks dont eat people. Some sharks do, some sharks don't. If I'm not mistaking, there are several kinds of Pings. There's an UDP Echo port (port 7) that can be used for pinging, plain Ping is over ICMP, etc. I think Net::Ping supports 3 types of pinging, being ICMP, UDP and TCP pings. But there are ofcourse dozens of other ways to detect if a host is online.

      Greetz
      Beatnik
      ...Perl is like sex: if you're doing it wrong, there's no fun to it.
Re: Pinging.
by Jaap (Curate) on Aug 07, 2002 at 15:24 UTC
    Ping doesn't use a port. I would not expect to have the monks almost write the script for you - just appreciate when they give you a good hint. Please don't abuse it. ---- To me, boxing is lake ballet. Except there's no music, no choreography and the dancers hit each other.