A Simple Bash Script to Reverse SSH Tunnel Automatically

While, this post will be very simple since it’s more like a note for my personal use. If you’d like to know what is the SSH Tunnel, what is the SSH port forwarding, or how to reverse SSH tunnel, please Google it.

The following script will try to connect to another server automatically every 10 seconds. Remember to run it using tmux or screen so it can stay in the background when you are disconnected.

while true;do
  RET=`ps ax | grep "ssh -f -N -R 19995:localhost:22" | grep -v "grep"`
  if [ "$RET" = "" ]; then
    echo "restart ssh server"
    ssh -f -N -R 19995:localhost:22 <username>@dslsrv1.rnet.missouri.edu
  sleep 10

Simply speaking, the above code will forward its own port 22 to port 19995 on dslsrv1.

comments powered by Disqus