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

Re: Remote host back online test

by quester (Vicar)
on Nov 27, 2007 at 07:46 UTC ( [id://653166]=note: print w/replies, xml ) Need Help??


in reply to Remote host back online test

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 sshping.pl 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 sshping.pl localhost; echo $? Connecting to port 22... connect() failed: Connection refused at sshping.pl 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 sshping.pl localhost; then echo up; else echo down; fi Connecting to port 22... connect() failed: Connection refused at sshping.pl line 14. down

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-03-28 18:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found