Keep Alive SSH Port Forwarding


Keep Alive SSH Port Forwarding


SSH Port Forwarding allows you to push encrypted traffic from your local machine to a remote machine using a specific port. For example, lets say you have a Raspberry Pi weather station and you want to log weather changes into a remote MySQL database but you don't want to open port 3306 to the world (It's just a bad idea anyway). This is accomplished by establishing an SSH tunnel, forwarding local port 3306 on your Raspberry Pi to the MySQL database over the normal SSH port 22. Your logging application connects to local port (on the Raspbery Pi) 3306 as if the MySQL instance were local. Its crazy, I know!

To make things a little easier for you, I've built a little script that will not only forward your desired port but also keep that forwarded port alive. Drop the following script into a file and make it executable.


echo Trying to create tunnel connection to $remote_ip at `date '+%y-%m-%d %H:%M'`
pid=$(cat /home/pi/
if [ ! -e /proc/${pid} -a /proc/$pid/exe ]; then
  echo `date '+%y-%m-%d %H:%M:%S'` "Creating new tunnel connection to $remote_ip"
  $($(/usr/bin/ssh -i /home/pi/.ssh/id_rsa user@$remote_ip -Ngv -L $local_port:$local_ip:$remote_port sleep 9999) & echo $! > /home/pi/ &)
  if [[ $? -eq 0 ]]; then
    echo `date '+%y-%m-%d %H:%M:%S'` "Tunnel to $remote_ip through $local_port --> $local_port created successfully"
    echo `date '+%y-%m-%d %H:%M:%S'` "ERROR: An error occurred creating a tunnel at `date '+%y-%m-%d %H:%M'` to $remote_ip through $local_port was $?"
The script accepts four parameters.
  • remote_ip = the remote system
  • remote_port = the port for the remote system
  • local_ip = the localhost interface you want to use
  • local_port = the local port you are forwarding to the remote_port
The IF statement is the Keep Alive. It check to see if the process is already running. If the tunnel isn't then we move into the IF statement and establish the ssh tunnel. The  magic happens on this line:
$($(/usr/bin/ssh -i /home/pi/.ssh/id_rsa user@$remote_ip -Ngv -L $local_port:$local_ip:$remote_port sleep 9999) & echo $! > /home/pi/ &)
Here is how you would call the script, passing the four parameters:
/home/pi/ 3306 localhost 3306

I will generally add this command to cron and have it run once every few minutes.

Happy Tunneling!

You might also like

Simple Bash Script To Email Server Status


I didn't want to constantly have to log into my servers in order to check on key performance indicators so I decided to write a simple script that would do the checking for me. After collecting results, the script emails them to me. There are a few tools called within the script you might need to install. I also convert any tabs into spaces in order to make sure things line up nicely inside my email. #!/bin/bash SERVER="myserver001" TOEMAIL="" FROMEMAIL="myserver001@myserverscom" # Who is logged in and what are they up to WHO=`w` #



How to fix: “PageHandlerFactory-Integrated” has a bad module when setting up ASP.NET


I was recently setting up IIS 7.5 on Windows 2008 R2 for an ASP.NET site and came across the following 500 error: Handler "PageHandlerFactory-Integrated" has a bad module "ManagedPipelineHandler" in its module list. After a bit of searching, it turns out ASP.NET was not completely installed with IIS even though I checked that box in the "Add Feature" dialog. I found a number of suggestions but found this command. It fixed my issues and got rid of the error. %windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -i For a 32 bit system, use the