4

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

2
nc -z $REMOTESERVER 22 
echo $?

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

1
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. – rs028 Aug 13 '12 at 15:57
-1
hping $REMOTESERVER -S -p 22 -c 2 >/dev/null 2>&1; echo $?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.