Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
nc -z <host> <port> From – RedGrittyBrick Aug 8 '12 at 11:44
Why don't you just run rsync? – AnonymousLurker Aug 8 '12 at 11:56
That is bizarre, I had tried nc yesterday and it did not work (it just hang there fro several minutes), but now it seems to work. So I guess nc -z $SERVN > /dev/null; echo $? should do the job. or even without redirection to /dev/null. – rs028 Aug 8 '12 at 13:23
I am not the sysadmin of that machine, but thanks, very useful link – rs028 Aug 13 '12 at 16:04
up vote 2 down vote accepted
echo $?

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

share|improve this answer
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.

share|improve this answer
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
hping $REMOTESERVER -S -p 22 -c 2 >/dev/null 2>&1; echo $?
share|improve this answer
and what does this do? – Sathya Oct 9 '12 at 8:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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