Making a .sh script to check if SSH connection exists and if not then connect
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):
ssh user@host -fTN -R 2222:127.0.0.1:22 -i $HOME/.ssh/id_rsa
- the options
-fTN
will push the connection into the background - I wrote this leading part, because this set of options is critical for my suggestion below; - the option
-R 2222:127.0.0.1:22
will create the reverse tunnel; - the option
-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:
#!/bin/bash
SSH_COMMAND="ssh user@host -fTN -R 2222:127.0.0.1:22 -i $HOME/.ssh/id_rsa"
if [[ -z $(ps -aux | grep "$SSH_COMMAND" | sed '$ d') ]]
then exec $SSH_COMMAND
fi
Call this script my_autossh
, place it in ~/bin
and make it executable. Then run crontab -e
and add the following job:
* * * * * $HOME/bin/my_autossh
If you do not want to use Cron, modify the scrip my_autossh
in this way:
#!/bin/bash
SSH_COMMAND="ssh user@host -fTN -R 2222:127.0.0.1:22 -i $HOME/.ssh/id_rsa"
while true; do
if [[ -z $(ps -aux | grep "$SSH_COMMAND" | sed '$ d') ]]
then eval $SSH_COMMAND
else sleep 60
fi
done
And use nohup
to push it into the background:
nohup my_autossh >/dev/null 2>&1 &
Read also:
Related videos on Youtube
Askerman
Updated on September 18, 2022Comments
-
Askerman over 1 year
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.
-
pa4080 almost 6 yearsI think you do not need such script, because
autossh
is already available :) Please review this answer: askubuntu.com/a/1006245/566421 -
Askerman almost 6 yearsUnfortunately, I do not have access to such fancy programs at the work computer (which is actually a NAS), it has to be through the .sh file.
-
pa4080 almost 6 yearsYou mean you do not have permissions to install it?
-
Askerman almost 6 yearsSomething like that, I can't install any extra packages. The whole thing simply has to be made from the .sh script and ssh command.
-
-
pa4080 almost 6 years@dessert, I'm not sure :)
ps -aux | grep "$SSH_COMMAND"
will return alsogrep --color=auto ssh user@host -fTN -R 2222:127.0.0.1:22 -i /home/user/.ssh/id_rsa
this is the reason why I cut the last line withsed '$ d'
. -
PerlDuck almost 6 years@dessert I just could edit the post myself but on the other hand you are right, too: not all
grep
s of all times were aware of the-q
switch. For instance, AIX 4.3 doesn't know about it as I just verified. The advantage of>/dev/null
is that it always works. -
pa4080 almost 6 years@dessert, nice! This works:
ps -aux | grep -q "[s]sh user@host -fTN -R 2222:127.0.0.1:22 -i /home/spas/.ssh/id_rsa" && echo true || echo false
. But I can't imagine how to implement the square brackets when I'm using a variable withgrep
. Another way is to use:pgrep -f "$SSH_COMMAND" >/dev/null && echo true || echo false
. -
pa4080 over 5 yearsProbably to use
grep -q
the syntax should beps -aux | grep -v 'grep' | grep -q "$SSH_COMMAND"