For the sake of this explanation, machine is my laptop, and I use it to connect to a remote server - which I will refer to as server_name.

The procedure can be described as follows:

  1. I connect to server server_name with ssh from machine
  2. I then inserts password for server_name
  3. I compile something on server_name
  4. I log out of server_name (using exit)
  5. I then reboot server_name from machine
  6. I ping server_name and then I wait until I get a response from it (ping's output is shown on the console)
  7. I ssh into server_name again
  8. I run some checks
  9. Finally, I exit to server_name in order to go back to machine

To accomplish the above recipe, I manually run the following commands from machine:

<machine>$ ssh server_name
insert password for server_name:
<server_name>$ cd /some/path/
<server_name>$ make clean > /dev/null
<server_name>$ make > /dev/null
<server_name>$ exit
<machine>$ ./my_reboot_script server_name
<machine>$ ping server_name
PING with 56(84) bytes of data.
ping output... (not very interesting)...
ping output... (not very interesting)...
ping output... (not very interesting)...
ping output... (not very interesting)...
<machine>$ ssh server_name
insert password for server_name:
<server_name>$ ./run_some_checks
<server_name>$ exit
<machine>$ echo "Done!"

Couple of notes on the above is in order:
- my_reboot_script is just a program that allows me to cold-reboot server_name from the comport of my own seat.
- After I cold-reboot server_name, I need to wait for it to go back up, so I can reconnect to it using ssh. To do that, I use ping; it is merely used as an indication to when server_name is ready to accept connections again; Only when I see the ping output finally spawning on the console, I interrupt it with Ctrl+C, and then I ssh into server_name.

Doing the above manually is tedious and time consuming. So I want to automate the above process, but there are three difficulties:

  1. While connected to server_name, issuing the exit command from the shell gracefully logs me out and returns me to machine. However, issuing exit from within a script .sh file will terminate the script...
  2. How can I automate the password to server_name phase? I don't want to manually typing the password, but rather let the script do it; in other words - I want to somehow hardcode the password into the script so it won't bother me with it.
  3. The "waiting-for-server-to-be-ready-for-ssh-connections" phase: How can I automate this? Meaning, instead of just pinging until I get a response, then interrupting with Ctrl+C, I want the script to ping, recognize that there is a response from server_name, then connect with ssh. Although, using ping might not be such a good idea here, since response to ping does not guarantee that server_name is ready to listen to ssh connections (from my experience with the above process, it takes a few more seconds from the moment the first response to the ping is obtained). Therefore, a better approach might be for the script to keep on trying to ssh in a loop until it granted access (some sort of a busy waiting mechanism, or even a loop that sleeps and wakes up every few seconds to try to connect).
  • 1
    regarding 2. - why not use key based authentication? – mateusz.kijowski Oct 30 '15 at 17:36

If you can get the public key data from your machine to the server's .ssh/authorized_key files, you could run the compilation with a single ssh invocation per server, without ever entering a password:

ssh someone@$SERVER 'cd /some/path/; make clean > /dev/null; make > /dev/null;'

You could put that in a loop in a shell script, changing the value of shell variable SERVER each time through. If you don't mind entering your password for every ssh invocation, you don't even need the public key data on the servers.

  • Hey Bruce Ediger, thanks for your answer. Appreciate it. Your elegant suggestion does solve 1. and 2. in my post, but this still doesn't solve 3. Do you have an idea on how to automate 3? – so.very.tired Oct 31 '15 at 9:25

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.