Kjetil's Information Center: A Blog About My Projects

Reverse SSH Tunnel Listener

The Reverse SSH Tunnel Launcher script I posted a while ago has some limitations. The worst part is that the tunnel is only open for 5 minutes, leaving too little time to get any work done. This is kind of by design, to prevent having these connections open and "live" when not in use.

The solution to this limitation is another small script, this time just hacked together as a Bourne shell script. Take a look:


while /bin/true; do
  if /bin/netstat -tln | fgrep > /dev/null; then
    ssh localhost -p 1337 screen -d -m ssh -v -R 1338:localhost:22 -N -p 22
    echo "New tunnel established!"
  sleep 10

This script will loop forever and wait for a socket to appear on the port (1337) opened by the original launcher. Once this happens, a new tunnel is created (on port 1338) in parallel which will persist forever through a screen session.

Topic: Scripts and Code, by Kjetil @ 02/02-2020, Article Link