Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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. –  rsom Aug 8 '12 at 13:23
    
    
I am not the sysadmin of that machine, but thanks, very useful link –  rsom Aug 13 '12 at 16:04
add comment

3 Answers

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

Your Answer

 
discard

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.