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

In reply to Re: Remote host back online test by quester
in thread Remote host back online test by RaduH

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.