I am struggling with writing what I assume should be an easy script.
Basically I have a computer at work, which is hidden behind a NAT to which I don't have access. I need to SSH into this computer, so the only way to do this is to set a reverse port redirect, where this NATted computer will connect to my server at home, while redirecting a port for it self and then I can simply ssh into it through my home server.
The .sh script will be executed by the computer at work every 5 minutes and this is what I would like it to do:
Check if there is an active ssh connection to my server going on and if yes, then simply do nothing and exit the script.
If there is no active connection detected, then connect by executing "ssh [email protected] -i key.priv" and exit the script.
If the ssh connection attempt hangs for some reason, for say longer than 2 minutes, then force exit the script (not sure if this is possible to do, if not, then it doesn't have to be there)
Thank you kindly for your advice.
Let's assume you are using the following command to establish your SSH connection (I would prefer to use
.ssh/config
file that will simplify the ssh command, but this is not mandatory):-fTN
will push the connection into the background - I wrote this leading part, because this set of options is critical for my suggestion below;-R 2222:127.0.0.1:22
will create the reverse tunnel;-i $HOME/.ssh/id_rsa
indicates the identity file.We can use
ps -aux | grep "<our command>" | sed '$ d'
to check whether the connection is established or not. Based on this our script could be:Call this script
my_autossh
, place it in~/bin
and make it executable. Then runcrontab -e
and add the following job:If you do not want to use Cron, modify the scrip
my_autossh
in this way:And use
nohup
to push it into the background:Read also: