TMBTC Post

Keep Alive SSH Port Forwarding


SSH SSH PORT FORWARDING

Keep Alive SSH Port Forwarding

2017-09-18
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.

#!/bin/bash
remote_ip=$1
remote_port=$2
local_ip=$3
local_port=$4

echo Trying to create tunnel connection to $remote_ip at `date '+%y-%m-%d %H:%M'`
pid=$(cat /home/pi/ssh_tunnel_params.pid)
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/ssh_tunnel_params.pid &)
  if [[ $? -eq 0 ]]; then
    echo `date '+%y-%m-%d %H:%M:%S'` "Tunnel to $remote_ip through $local_port --> $local_port created successfully"
  else
    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 $?"
  fi
fi
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/ssh_tunnel_params.pid &)
Here is how you would call the script, passing the four parameters:
/home/pi/ssh_tunnel_params.sh remotesystem.com 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

Simple Bash Script To Email Server Status

2012-08-22

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="admin@myservers.com" FROMEMAIL="myserver001@myserverscom" # Who is logged in and what are they up to WHO=`w` #


Read More...

how-to-fix-pagehandlerfactory-integrated-has-a-bad-module-when-setting-up-asp-net

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

2012-08-22

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


Read More...