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:
- I connect to server server_name with
- I then inserts password for server_name
- I compile something on server_name
- I log out of server_name (using
- I then reboot server_name from machine
pingserver_name and then I wait until I get a response from it (
ping's output is shown on the console)
sshinto server_name again
- I run some checks
- Finally, I
exitto 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 10.23.22.7 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)... ^C <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:
- While connected to server_name, issuing the
exitcommand from the shell gracefully logs me out and returns me to machine. However, issuing
exitfrom within a script
.shfile will terminate the script...
- 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.
- 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
pingmight not be such a good idea here, since response to
pingdoes not guarantee that server_name is ready to listen to
sshconnections (from my experience with the above process, it takes a few more seconds from the moment the first response to the
pingis obtained). Therefore, a better approach might be for the script to keep on trying to
sshin 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).