S
S
S1ashka2012-06-14 23:17:57
linux
S1ashka, 2012-06-14 23:17:57

how to create a loop with execution after the completion of the command?

I apologize for a simple and stupid question
, there is a piece of iron with 3g, located in a remote corner. knowing the stability of our mobile Internet - the connection can sometimes break
to the piece of iron, constant access is needed
for this, a reverse shell is created via ssh
ssh -R localhost:2222:localhost:22 [email protected]

in bashscripting, I'm completely zero and unfortunately there is no time to figure it out , I
think you need a cycle with the execution of this command. how to make it run in case of disconnect?

thank you in advance

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
RainWarrior, 2012-06-15
@S1ashka

autossh
Holds an additional connection over which it sends a Keep-alive for verification. Available in ubuntu and debian repositories

@
@sledopit, 2012-06-14
_

In general:

while :
do
ssh -R localhost:2222:localhost:22 [email protected]
done

However, in this particular case, there is a more elegant solution:
there is an autossh program created just for such conditions (automatic reconnect after connection loss).
Just install the package, change ssh to autossh in the command and it will be the way you want (:

G
gouranga, 2012-06-15
@gouranga

As far as I remember, there is inittab. Write there something like

tn:2345:respawn:/usr/bin/ssh -n -R localhost:2222:localhost:22 [email protected]

and do kill -1 1.

V
Vlad Zhivotnev, 2012-06-15
@inkvizitor68sl

cat > /usr/bin/connect-somewhere.sh
#!/bin/bash
while :; do ssh -R localhost:2222:localhost:22 [email protected]; date; sleep 20; done
chmod +x /usr/bin/connect-somewhere.sh
And in /etc/crontab:
* * * * * root flock -n /var/lock/somelock.lock -c /usr/bin/connect-somewhere.sh
Run and don't sweat it. An open ssh session will prevent the loop from continuing. When the session falls off due to a timeout, the script will display the date (of course, it is better in the log), wait 20 seconds, and try to connect again. Cron won't let that thing die.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question