LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash script to test if I can SSH to a server? (https://www.linuxquestions.org/questions/linux-general-1/bash-script-to-test-if-i-can-ssh-to-a-server-554923/)

DanHulton 05-18-2007 10:16 AM

Bash script to test if I can SSH to a server?
 
I have an environment when I lauch three virtual machines, then mount them via sshfs. I want to write a bash script to launch all three machines and mount them automatically.

My original plan was to use ping to identify when the servers were up, but they resolve via ping almost instantly. SSH login is only available later.

The vast majority of the script is functioning, the only part I'm missing is a section to test if I can SSH into the machine yet or not.

Psuedo-code:

Code:

Loop
  Status message: Waiting for server to finish startup...
Until SSH is available.


anomie 05-18-2007 10:49 AM

Check if your netcat implementation supports the -z (list only, don't send data) option. If not, never mind. If so, you can use something like the following:
Code:

$ while true ; do nc -z 10.0.50.50 22 && break; echo 'Waiting for remote sshd...'; done
edit: You can add a sleep in there if needed.

DanHulton 05-18-2007 01:42 PM

Thanks, that worked perfectly!


All times are GMT -5. The time now is 02:08 PM.