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

Hi All,

I have got three servers i need to check to see whether they are alive or not. Is it possible to just ping each one of the servers, and if its alive just print 'alive', and if its dead, just print 'dead'?

I must admit that i am no perl programmer, (infact i havnt got a clue), but i understand from a friend that this should be pretty easy to do.

Your help is greatly appreciated.

Replies are listed 'Best First'.
Re: Is My Server Up??
by Joost (Canon) on Sep 09, 2002 at 15:34 UTC
    Since you cannot install Net::Ping, how about:
    if (`ping -c 1 my.test.com` =~ /1 recieved/) { print "Alive!\n"; } else { print "Dead\n"; }
    -- Joost downtime n. The period during which a system is error-free and immune from user input.
      Hi There

      I just tried the code and when i replace my.test.com with localhost and run the script it tells me that the system is dead....
      Sorry, i just realised the 'recieved' typo. Just one more question, is there any way to do a tcp connect rather than a ping?
        Assuming your server is running a web-server:
        use IO::Socket::INET; $sock = IO::Socket::INET->new( PeerAddr => 'www.mysite.com', PeerPort => 80, Proto => 'tcp'); if ($sock) { print "yep"; } else { print "nope"; }


        --twerq
        Nope. Just changed the spelling and it is still reporting that the server is down, even tho when i run the command it comes back with 1 packets received
Re: Is My Server Up??
by thelenm (Vicar) on Sep 09, 2002 at 15:27 UTC
    Check out Net::Ping. You can use it to do exactly what you need.

    -- Mike

    --
    just,my${.02}

      Oops, sorry.

      I forgot to meantion that i was informed that i could not add extra modules to perl....at least thats what i think my sysadmin said.
Re: Is My Server Up??
by hiseldl (Priest) on Sep 09, 2002 at 16:57 UTC
      I must admit that i am no perl programmer, (in fact i havn't got a clue), but i understand from a friend that this should be pretty easy to do.

    Take a look at Webmin.

    This systems administration interface could be a life saver for you. Check out the "System and Server Status" section, you should be able to set up a server "watch" pretty easily. The only hurdle would be setting this up, but there is a very nice setup script to help you. And, it is written in Perl! :-)

    Update: grrr, I just saw your update for no mods. :( Ask your sysadmin to install webmin, and give you access to it so that you can set up a watch. TMTOWTDI.

    --
    hiseldl
    "Act better than you feel" --inner prophet