0

I have a host that I need to deploy on a network that I will not have physical access to for some time, so I have set up a persistent reverse ssh connection from a guide, using cron and the script below:

#!/bin/bash

# install with crontab -e
# */1 * * * * /root/scripts/ssh_tunnel.sh > tunnel.log 2>&1

createTunnel() {
  /usr/bin/ssh -i /home/user/cert.pem -N -R 9999:localhost:22 user@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

The cron job is simply:

crontab -e
*/1 * * * * /root/scripts/ssh_tunnel.sh > tunnel.log 2>&1

I need ssh sessions to re-establish automatically in the event of interruption. Currently if the sshd process on the server is terminated, the process on the client does not terminate, with the result that the script does not try to establish a new session.
I notice that when the connection is started manually from an interactive terminal there is no issue, that is, the client dies along with the server-side process on termination.

Any help would be appreciated, thanks.

0 Answers0