Script for ssh port forwarding

Day to day I need to use ssh port forwarding and connect securely to my work servers. So I decided to create some simple bash script which will allow me to forward port via ssh and to kill previous tunnel if it freezed.

Script source:


#!/bin/bash
# irc.sh
kill -9 `ps -aef | grep -i 'ssh -f -N -L 1234:127.0.0.1:1234 user@hostname.com' | grep -v grep | awk '{print $2}'`
ssh -f -N -L 1234:127.0.0.1:1234 user@hostname.com

You can save this source code to any .sh file and run it manually from a command line, like this:


cd <script destination>
./irc.sh

I didn’t want to do it only manually so I created a shortcut on my desktop to be able to run it quickly:

Note, that I’m using key based SSH authentication.
My OS is Linux Mint 13.