Test if Remote server is up and running before SFTP'ing files (in batch mode)

Hello,

In our Data Warehouse environment, before our batch SFTP jobs kick off to pull the files from remote servers, I would like to setup a pre-sftp job that would test if all the remote servers from where the files are being pulled, are up and running. If any one of the remote serer is down, then an error needs to be raised.

I was thinking if I can use one of the below commands, but do not know what would be the return code I need to check in case of a successful connection and unsuccessful connection for these commands.
Please advise me of the appropriate commands that I could use in a shell script to handle this. Thank you

Commands:

  • sftp <host> OR sftp <username>@<host>
  • ping <host>
  • telnet <host> <port:22>
  • <any other command>?

Regards,
Dippu

How about

ping -c2 <host>
if [ $? = 0 ]
  then echo "It worked!"
else
  echo "Cant ping at <host>"
fi
1 Like

The bash internal /dev/tcp could also be used something like this:

if timeout 3 bash -c '> /dev/tcp/<host>/22' 2>/dev/null
then
    echo "ssh running on <host>"
else
   echo "Timeout connecting to port  ssh(22) on <host>"
fi