Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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
2  
nc -z <host> <port> From stackoverflow.com/a/9463554/477035 – RedGrittyBrick Aug 8 '12 at 11:44
3  
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
nc -z $REMOTESERVER 22 
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
4  
and what does this do? – Sathya Oct 9 '12 at 8:12

You must log in to answer this question.

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