Making a .sh script to check if SSH connection exists and if not then connect

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
2
down vote

favorite












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 me@1.2.3.4 -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.







share|improve this question




















  • I think you do not need such script, because autossh is already available :) Please review this answer: askubuntu.com/a/1006245/566421
    – pa4080
    Apr 26 at 9:52










  • Unfortunately, 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.
    – Askerman
    Apr 26 at 10:02











  • You mean you do not have permissions to install it?
    – pa4080
    Apr 26 at 10:11










  • Something like that, I can't install any extra packages. The whole thing simply has to be made from the .sh script and ssh command.
    – Askerman
    Apr 26 at 10:15














up vote
2
down vote

favorite












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 me@1.2.3.4 -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.







share|improve this question




















  • I think you do not need such script, because autossh is already available :) Please review this answer: askubuntu.com/a/1006245/566421
    – pa4080
    Apr 26 at 9:52










  • Unfortunately, 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.
    – Askerman
    Apr 26 at 10:02











  • You mean you do not have permissions to install it?
    – pa4080
    Apr 26 at 10:11










  • Something like that, I can't install any extra packages. The whole thing simply has to be made from the .sh script and ssh command.
    – Askerman
    Apr 26 at 10:15












up vote
2
down vote

favorite









up vote
2
down vote

favorite











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 me@1.2.3.4 -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.







share|improve this question












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 me@1.2.3.4 -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.









share|improve this question











share|improve this question




share|improve this question










asked Apr 26 at 9:25









Askerman

351513




351513











  • I think you do not need such script, because autossh is already available :) Please review this answer: askubuntu.com/a/1006245/566421
    – pa4080
    Apr 26 at 9:52










  • Unfortunately, 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.
    – Askerman
    Apr 26 at 10:02











  • You mean you do not have permissions to install it?
    – pa4080
    Apr 26 at 10:11










  • Something like that, I can't install any extra packages. The whole thing simply has to be made from the .sh script and ssh command.
    – Askerman
    Apr 26 at 10:15
















  • I think you do not need such script, because autossh is already available :) Please review this answer: askubuntu.com/a/1006245/566421
    – pa4080
    Apr 26 at 9:52










  • Unfortunately, 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.
    – Askerman
    Apr 26 at 10:02











  • You mean you do not have permissions to install it?
    – pa4080
    Apr 26 at 10:11










  • Something like that, I can't install any extra packages. The whole thing simply has to be made from the .sh script and ssh command.
    – Askerman
    Apr 26 at 10:15















I think you do not need such script, because autossh is already available :) Please review this answer: askubuntu.com/a/1006245/566421
– pa4080
Apr 26 at 9:52




I think you do not need such script, because autossh is already available :) Please review this answer: askubuntu.com/a/1006245/566421
– pa4080
Apr 26 at 9:52












Unfortunately, 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.
– Askerman
Apr 26 at 10:02





Unfortunately, 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.
– Askerman
Apr 26 at 10:02













You mean you do not have permissions to install it?
– pa4080
Apr 26 at 10:11




You mean you do not have permissions to install it?
– pa4080
Apr 26 at 10:11












Something like that, I can't install any extra packages. The whole thing simply has to be made from the .sh script and ssh command.
– Askerman
Apr 26 at 10:15




Something like that, I can't install any extra packages. The whole thing simply has to be made from the .sh script and ssh command.
– Askerman
Apr 26 at 10:15










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










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 &





share|improve this answer






















  • @dessert, I'm not sure :) ps -aux | grep "$SSH_COMMAND" will return also grep --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 with sed '$ d'.
    – pa4080
    Apr 26 at 11:41











  • @dessert I just could edit the post myself but on the other hand you are right, too: not all greps 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.
    – PerlDuck
    Apr 26 at 12:08











  • @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 with grep. Another way is to use: pgrep -f "$SSH_COMMAND" >/dev/null && echo true || echo false.
    – pa4080
    Apr 26 at 14:07











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1028338%2fmaking-a-sh-script-to-check-if-ssh-connection-exists-and-if-not-then-connect%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










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 &





share|improve this answer






















  • @dessert, I'm not sure :) ps -aux | grep "$SSH_COMMAND" will return also grep --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 with sed '$ d'.
    – pa4080
    Apr 26 at 11:41











  • @dessert I just could edit the post myself but on the other hand you are right, too: not all greps 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.
    – PerlDuck
    Apr 26 at 12:08











  • @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 with grep. Another way is to use: pgrep -f "$SSH_COMMAND" >/dev/null && echo true || echo false.
    – pa4080
    Apr 26 at 14:07















up vote
2
down vote



accepted










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 &





share|improve this answer






















  • @dessert, I'm not sure :) ps -aux | grep "$SSH_COMMAND" will return also grep --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 with sed '$ d'.
    – pa4080
    Apr 26 at 11:41











  • @dessert I just could edit the post myself but on the other hand you are right, too: not all greps 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.
    – PerlDuck
    Apr 26 at 12:08











  • @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 with grep. Another way is to use: pgrep -f "$SSH_COMMAND" >/dev/null && echo true || echo false.
    – pa4080
    Apr 26 at 14:07













up vote
2
down vote



accepted







up vote
2
down vote



accepted






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 &





share|improve this answer














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 &






share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 26 at 18:42

























answered Apr 26 at 11:14









pa4080

12k52255




12k52255











  • @dessert, I'm not sure :) ps -aux | grep "$SSH_COMMAND" will return also grep --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 with sed '$ d'.
    – pa4080
    Apr 26 at 11:41











  • @dessert I just could edit the post myself but on the other hand you are right, too: not all greps 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.
    – PerlDuck
    Apr 26 at 12:08











  • @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 with grep. Another way is to use: pgrep -f "$SSH_COMMAND" >/dev/null && echo true || echo false.
    – pa4080
    Apr 26 at 14:07

















  • @dessert, I'm not sure :) ps -aux | grep "$SSH_COMMAND" will return also grep --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 with sed '$ d'.
    – pa4080
    Apr 26 at 11:41











  • @dessert I just could edit the post myself but on the other hand you are right, too: not all greps 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.
    – PerlDuck
    Apr 26 at 12:08











  • @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 with grep. Another way is to use: pgrep -f "$SSH_COMMAND" >/dev/null && echo true || echo false.
    – pa4080
    Apr 26 at 14:07
















@dessert, I'm not sure :) ps -aux | grep "$SSH_COMMAND" will return also grep --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 with sed '$ d'.
– pa4080
Apr 26 at 11:41





@dessert, I'm not sure :) ps -aux | grep "$SSH_COMMAND" will return also grep --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 with sed '$ d'.
– pa4080
Apr 26 at 11:41













@dessert I just could edit the post myself but on the other hand you are right, too: not all greps 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.
– PerlDuck
Apr 26 at 12:08





@dessert I just could edit the post myself but on the other hand you are right, too: not all greps 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.
– PerlDuck
Apr 26 at 12:08













@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 with grep. Another way is to use: pgrep -f "$SSH_COMMAND" >/dev/null && echo true || echo false.
– pa4080
Apr 26 at 14:07





@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 with grep. Another way is to use: pgrep -f "$SSH_COMMAND" >/dev/null && echo true || echo false.
– pa4080
Apr 26 at 14:07


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1028338%2fmaking-a-sh-script-to-check-if-ssh-connection-exists-and-if-not-then-connect%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

pylint3 and pip3 broken

Missing snmpget and snmpwalk

How to enroll fingerprints to Ubuntu 17.10 with VFS491