Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Remote host back online test

by RaduH (Scribe)
on Nov 26, 2007 at 18:39 UTC ( #653059=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,

I am executing some script on HostA and this script sends commands to HostB. At some point, HostB is being reset and after it comes back online there are more commands that need to be sent by HostA's script to HostB. This means HostA's script needs to figure somehow when it can send the commands to HostB again (the commands are sent using the unix command line ssh with the [command] parameter)

There are multiple ways to figure out if HostB is ready to take more commands after a reset but I was wondering what is THE way to do it, if there is such a thing. I was thinking about pinging HostB until I see it is alive (don't really know how to pick up the ping response in my Perl script since ping runs until I stop it and keeps spitting out data, but that's a story for me to worry about only if this is THE way to go). I was also thinking about attempting to run some test command the result of which I would recognize if HostB was running again (e.g. If I can execute some perl and get OK back I know HostB is alive).

Are there other options? Is there a smarter of way doing this?


Replies are listed 'Best First'.
Re: Remote host back online test
by Corion (Pope) on Nov 26, 2007 at 18:44 UTC

    I would use something like Net::Ping or simply the external ping utility. Most of the external ping utilities have a way of only checking once or a short time whether a host is reachable, for example the Solaris and Win32 ping commands, so using that should be easy as well.

Re: Remote host back online test
by andyford (Curate) on Nov 26, 2007 at 21:39 UTC

    Pinging might not work well for you. Computers tend to be 'pingable' soon after they boot up, but other services such as a database or webserver might start considerably later. You might want to brute force it: just send your command and try to analyze the result and resend if necessary. Or perhaps figure out some other test that tells you that the service you need is running.

Re: Remote host back online test
by chrism01 (Friar) on Nov 27, 2007 at 02:01 UTC
    As per andyford, ping only tells you the network stack/code is up. You really need an application specific test to tell you if the app is working. eg for a DB it'd be something like 'select now()'


Re: Remote host back online test
by quester (Vicar) on Nov 27, 2007 at 07:46 UTC
    If you want to test whether the SSH service on the remote host is ready to accept logins you can try connecting to TCP port 22 on the remote host and see if you can read the SSH banner from it. I adapted this example from the perlipc documentation:
    use warnings; use strict; use Socket; my ($remote,$port, $iaddr, $paddr, $proto, $line); $remote = $ARGV[0] || die "usage: $0 hostname"; $port = 22 ; # the SSH port $iaddr = inet_aton($remote) || die "no such host: $remote"; $paddr = sockaddr_in($port, $iaddr); $proto = getprotobyname('tcp'); socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket() failed: $! +"; print "Connecting to port 22...\n"; connect(SOCK, $paddr) || die "connect() failed: $!"; print "Connected.\n"; $line = <SOCK>; print $line; exit 0 if $line =~ /SSH/; exit 1;
    With the SSH server on my box running:
    $ perl localhost; echo $? Connecting to port 22... Connected. SSH-1.99-OpenSSH_x.xx Debian-xxx 0
    If I stop the SSH server on my box:
    $ perl localhost; echo $? Connecting to port 22... connect() failed: Connection refused at line 14. 111
    You might want to have the perl script loop (with a sleep for a few seconds) and retry the connect, or you could do that in an external bash script by checking the exit code...
    $ if perl localhost; then echo up; else echo down; fi Connecting to port 22... connect() failed: Connection refused at line 14. down

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://653059]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2020-10-28 06:35 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (260 votes). Check out past polls.