16

I'm writing a script that will backup data from my laptop to an Ubuntu server. To do so, I'm looking for a (ba)sh command to test if the server is available before starting the backup. something like ping on port 22 that returns a boolean.

How can I do this?

28

Like this:

nc -z hostname 22 > /dev/null
echo $?

If it's 0 then it's available. If it's 1 then it's not.

5
  • Note that this option works only with the netcat-openbsd package, otherwise the option is not present: nc: invalid option -- 'z' Jan 18 '17 at 7:26
  • 2
    without -w option nc will be stuck forever on Ubuntu Jan 21 '19 at 10:09
  • can we have a similar command from windows Mar 26 '20 at 19:31
  • This tests if port $host:22 is open, not if a sshd is taking requests, which means a false positive on any/all proxied requests. Just timeout ssh if you want a valid/reliable check timeout1 ssh blah or ssh -o ConnectTimeout=1 blah Nov 24 '20 at 16:03
  • In my case the nc cmd was still outputting, have to use &> /dev/null to silence it.
    – robbash
    Mar 21 at 9:22
14

Use netcat:

nc -z localhost 22

From the manpage

 -z      Specifies that nc should just scan for listening daemons,
         without sending any data to them.
4
  • Damn! You beat me to it.
    – bahamat
    Feb 2 '11 at 20:23
  • 2
    thanks, didn't know nc , i was wondering why nc -z -w 2 192.168.0.123 ( -w is the timeout option, in seconds ) does not timeout :|
    – Amo__
    Feb 2 '11 at 23:12
  • You need to specify a port too. nc -z -w 2 192.168.0.123 22 works as expected.
    – chris
    Feb 3 '11 at 7:36
  • +1 for the z into. whoever you scan still can get info of who did the scan only without providing the identification string
    – amrx
    Sep 11 '16 at 20:37
6

What about

MACHINE=192.168.0.8
exec 3>/dev/tcp/${MACHINE}/22
if [ $? -eq 0 ]
then
    echo "SSH up"
else
    echo "SSH down"
fi
5
  • Don't always assume that it's Linux. His laptop may be a Mac. And Debian disables /dev/tcp. Ubuntu being Debian derived may as well (but I have no specific knowledge). But if /dev/tcp is available, then yes this is perfectly valid.
    – bahamat
    Feb 2 '11 at 20:35
  • thx for your feedback, i've wrote it on a Mac.
    – user4418
    Feb 2 '11 at 20:49
  • thanks, even if exec 3>/dev/tcp/${HOST}/22 looks really "bizarre"
    – Amo__
    Feb 2 '11 at 23:17
  • 2
    This is the only answer that doesn't require to install additional packages.
    – Chen A.
    Oct 23 '17 at 13:14
  • Works great for my use case in Mac and Manjaro (Arch Linux) Aug 25 '20 at 18:10
6

Alternate:

nc -vzw 1 hostname 22
2
  • this should be selected answer, without -w option nc will be stuck forever on Ubuntu Jan 21 '19 at 10:10
  • Bool can be used like => nc -zw 2 examplehost.com 22 && { echo "You can call your backup function" ; } || { echo "SSH Unavailable" ; }
    – Mr. Pundir
    Feb 23 '19 at 11:38

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.