On a remote lab system, I have a shell script that initiates a reverse SSH tunnel to my main jumpbox that gets run with a cron job every 5 minutes. If the SSH tunnel is up nothing happens, if it is down it initiates it.
#!/bin/bash
createTunnel() {
/usr/bin/ssh -N -R :2222:localhost:22 username@jumpbox.example.com
if [[ $? -eq 0 ]]; then
echo Tunnel to jumpbox created successfully
else
echo An error occurred creating a tunnel to jumpbox. RC was $?
fi
}
/bin/pidof ssh
if [[ $? -ne 0 ]]; then
echo Creating new tunnel connection
createTunnel
fi
This has been extremely reliable to ensure my access to the remote machine if it gets rebooted or as my jumpbox IP changes. However, I recently added a second SSH tunnel to this system and had a situation where one of the two tunnels went down and was never re-established. It appears that since there was one tunnel up, the pidof output still returned with a PID so the script never ran "createTunnel". Since I have two SSH tunnels, the pidof output shows both PID's:
$ /bin/pidof ssh
28281 28247
How can I adjust my script to determine if only one of the tunnels is down?
autossh -f ...that never exits. It's not clear whether the two connections are to the same endpointusername@jumpbox.example.com. If so, a singlessh(orautossh) can create both tunnels, just use additional-Ror-L. – Kamil Maciorowski Jan 02 '18 at 06:05