I improved Steveas answer by adding a timeout (here 2 secs):
ssh -o ConnectTimeout=2 -p <port> <hostname> exit 2>/dev/null ; echo $?
If port is the default 22 then you can run just:
ssh -o ConnectTimeout=2 <hostname> exit 2>/dev/null ; echo $?
Without timeout ssh
may wait a long time.
Using ssh
instead of nc
has a big advantage: It checks that answering daemon is ssh and not just any daemon. Successful "ssh ping" with ssh
most likely indicates that the real ssh connection will succeed.
NOTE also that there is available app called ssh-ping
. It is part of ssh-tools