ssh tunnel with auto reconnect ability

while [ ! -f /tmp/stop ]; do ssh -o ExitOnForwardFailure=yes -R 2222:localhost:22 target "while nc -zv localhost 2222; do sleep 5; done"; sleep 5;done

13
By: jianingy
2009-02-08 12:45:43

These Might Interest You

  • A quick alias I use right before logging into a server so that I have a log of the transactions as well as the ability to re-connect from another computer. Useful for when your boss says "what commands did you run again on that server?" and you had already closed the terminal ;) I wrapped it in a script now, with more features, but this is the heart of it. Never leave home without it.


    1
    alias m='screen -S $$ -m script'
    robinsonaarond · 2015-10-01 18:07:18 1
  • Long before tabbed terminals existed, people have been using Gnu screen to open many shells in a single text terminal. Combined with ssh, it gives you the ability to have many open shells with a single remote connection using the above options. If you detach with "Ctrl-a d" or if the ssh session is accidentally terminated, all processes running in your remote shells remain undisturbed, ready for you to reconnect. Other useful screen commands are "Ctrl-a c" (open new shell) and "Ctrl-a a" (alternate between shells). Read this quick reference for more screen commands: http://aperiodic.net/screen/quick_reference


    29
    ssh -t user@some.domain.com /usr/bin/screen -xRR
    olifante · 2009-02-20 23:39:54 0
  • This obtains a list of open connections that a user is connected to if he/she is using a SSH tunnel


    -2
    netstat -t -p --extend | grep USERNAME
    wr8cr8 · 2012-08-07 02:25:54 3
  • This command will: 1. open an SSH tunnel to 2. go to background 3. wait for 10 seconds for the connection 4. during the 10 seconds wait it will localy run 'rdesktop' to connect to the remote host through the created SSH tunnel. Password-less log in can be achieved (when server allows it) by adding '-p ' to the 'rdesktop' command


    12
    ssh -f -L3389:<RDP_HOST>:3389 <SSH_PROXY> "sleep 10" && rdesktop -T'<WINDOW_TITLE>' -uAdministrator -g800x600 -a8 -rsound:off -rclipboard:PRIMARYCLIPBOARD -5 localhost
    johnquail · 2011-07-14 05:48:06 0

What Others Think

check out the -N option to ssh Do not execute a remote command. This is useful for just forwarding ports (protocol version 2 only).
devnull · 488 weeks and 3 days ago
You might want to use ~/stop or ~/.stop as the file to stop your loop, since it's usually possible for anyone on the system to reach /tmp (doesn't matter on a single-user box, but it's generally a good idea to stay in your home directory anyway).
mulad · 487 weeks and 1 day ago
Or just run the who shebang out of /etc/inittab with the "respawn" option. See http://www.deer-run.com/~hal/sysadmin/SSH-SyslogNG.html
Hal_Pomeranz · 460 weeks and 4 days ago
Dunno about this "target" piece in your command. I assume your command is meant to provide a way to keep trying to connect if it fail. Inspired of this, I wrote my own command that help me automatically reconnect immediately after the connecting pipe is broken: while true; do if ! nc -z localhost 7070 > /dev/null; then ssh -N -D 127.0.0.1:7070 -p 22 someone@somesite.com; else echo 'not connected, retry after 10 seconds...'; fi; sleep 10; done
fwonce · 263 weeks and 6 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: