Friday 19 November 2010

Keeping an SSH tunnel (passworded) running perniciously

* Download and compile sshpass and place in DIR
http://sshpass.sourceforge.net/

* Create a script like this
DIR=/?
LOG=$DIR/ssh.log
LOCAL_PORT=15000
REMOTE_SERVER=192.168.69.69

REMOTE_PORT=1521
echo "`date` - Restarting " >> $LOG

export SSHPASS='PASSWORD'
$DIR/sshpass -e ssh  $USERNAME@ $REMOTE_SERVER -L${LOCAL_PORT}:localhost:${REMOTE_PORT} -N -o ServerAliveInterval=30 -o TCPKeepAlive=yes -o ServerAliveCountMax=172800
echo "`date` - Ending " >> $LOG

* Add the following to /etc/inittab - replace xxxx with a unique id, USER with the user to login as and and script.sh accordingly with full path
xxxx:35:respawn:/bin/su - USER "/?/script.sh"

* Get the init process to reload (as root)
kill -HUP 1

* Test

Thursday 18 November 2010

Keeping Your SSH Sessions Alive Through Firewalls

To send a keep alive every 4 minutes either change ~/.ssh/config or /etc/ssh/ssh_config to add
Host *   
     ServerAliveInterval 240