I have a bash script that uses rsync to backup files from two remote workstations (unix and linux) to a local hard drive. I want the script to check that the workstation is online before running rsync. The easiest way I could think of is to use ping:

ping -s 1 -c 1 $SERVN > /dev/null; echo $?

This works fine for one workstation, but for security reasons, pinging is disabled on the other one. I tried nc and it also does not work. I also tried to "ping" the machine using ssh or rsync, but of course I get a password prompt, which kind of defies the purpose of doing this in the first place. Anybody got any suggestion/idea? thanks


3 Answers 3

echo $?

If you are using SSH to do rsync that port should be open.

ssh $REMOTESERVER -o PasswordAuthentication=no

This should try to connect via SSH, but will not prompt for password, as that method is disabled by the option.

Caveat: this connection attempt may be counted as "unsuccessful login", and you could lockyourself out e.g. if you're using DenyHosts.

  • It seems that the easiest way it to use nc -z as suggested by RedGrittyBrick and Goblin, but thanks for this tip, it is going to be useful as well.
    – point618
    Commented Aug 13, 2012 at 15:57
hping $REMOTESERVER -S -p 22 -c 2 >/dev/null 2>&1; echo $?
  • 4
    and what does this do?
    – Sathyajith Bhat
    Commented Oct 9, 2012 at 8:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .