Ssh – How to check that a remote computer is online for ssh / script access

backupnetworkingpingscriptingssh

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?

Best Answer

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.

Related Question