You are not logged in.

#1 2012-02-04 21:22:26

hsk81
Member
Registered: 2012-02-04
Posts: 3

RC Script for SSH tunnel

Hello,

I'm often using an SSH tunnel to connect to the internet; since I got tired of typing almost every time I log into my system, I wrote an RC script which does the job for me. Here is the /etc/rc.d/ssh-tunnel script

#!/bin/bash

daemon_name=$(basename $0)

. /etc/rc.conf
. /etc/rc.d/functions
. /etc/conf.d/$daemon_name.conf

case "$1" in
	start)
		stat_busy "Starting $daemon_name daemon"

		if [[ -f /var/run/$daemon_name.pid ]]; then
			stat_fail ; exit 1
		fi

		if [[ -z $REMOTE_USER ]]; then
			stat_fail ; exit 1
		fi
		if [[ -z $REMOTE_ADDR ]]; then
			stat_fail ; exit 1
		fi
		if [[ -z $REMOTE_PORT ]]; then
			stat_fail ; exit 1
		fi
		if [[ -z $LOCAL_PORT ]]; then
			stat_fail ; exit 1
		fi

		SSH_HOST=${REMOTE_ADDR/:*/}
		SSH_PORT=${REMOTE_ADDR/*:/};SSH_PORT=${SSH_PORT/$SSH_HOST/}

		REMOTE=$REMOTE_USER@$SSH_HOST
		TUNNEL=$LOCAL_PORT:$SSH_HOST:$REMOTE_PORT

		if [[ -z $SSH_PORT ]]; then
			ssh $REMOTE -L $TUNNEL -N &
		else
			ssh $REMOTE -L $TUNNEL -N -p $SSH_PORT &
		fi

		if [[ $? -gt 0 ]]; then
			stat_fail ; exit 1
		fi

		echo $! > /var/run/$daemon_name.pid
		add_daemon $daemon_name
		stat_done
		;;

	stop)
		stat_busy "Stopping $daemon_name daemon"

		if [[ ! -f /var/run/$daemon_name.pid ]]; then
			stat_fail ; exit 1
		fi

		PID=$(cat /var/run/$daemon_name.pid)
		[[ -n $PID ]] && kill $PID &> /dev/null

		if [[ $? -gt 0 ]]; then
			stat_fail ; exit 1
		fi

		rm -f /var/run/$daemon_name.pid &> /dev/null
		rm_daemon $daemon_name
		stat_done
		;;

	restart)
		$0 stop
		sleep 3
		$0 start
		;;

	status)
		stat_busy "Checking $daemon_name status";
		ck_status $daemon_name
		;;

	*)
		echo "usage: $0 {start|stop|restart|status}"
esac

exit 0

with a corresponding /etc/conf.d/ssh-tunnel.conf configuration

REMOTE_USER="user"
REMOTE_ADDR="host"
REMOTE_PORT="8888"
LOCAL_PORT="8888"

or

REMOTE_USER="user"
REMOTE_ADDR="host:22"
REMOTE_PORT="8888"
LOCAL_PORT="8888"

I run on the server sshd and have a password-less login (using PGP key files); otherwise the script above does not make much sense since you don't want to enter passwords while your laptops boots. The configurations are equivalent to the following command line

ssh user@host -L 8888:host:8888 -N &

or

ssh user@host -L 8888:host:8888 -N -p 22 &

This sets up an encrypted connection between my proxy on my server running an port 8888 and connects it to the local port 8888 on my laptop. If you have a SSH port different than 22, then you should use the latter version, otherwise the first configuration.

Enjoy and report any bugs, please!

Offline

#2 2012-02-04 21:24:55

hsk81
Member
Registered: 2012-02-04
Posts: 3

Re: RC Script for SSH tunnel

BTW: You can create symbolic links like /etc/rc.d/sst0 to /etc/rc.d/ssh-tunnel (and the corresponding /etc/conf.d/sst0.conf configuration) if you wish to use more than one SSH tunnel.

Offline

#3 2012-04-03 19:29:41

Leonid.I
Member
From: Aethyr
Registered: 2009-03-22
Posts: 999

Re: RC Script for SSH tunnel

I usually use screen(1) to capture info/debug msg from the tunnel. For example:

screen -d -m -S ntc-tunnel ssh -YNMS /tmp/ssh-%r@%h:%p -D $socks_port  -L $ntcgrid_bind_port:ntcgrid:22 ntc

Arch Linux is more than just GNU/Linux -- it's an adventure
pkill -9 systemd

Offline

#4 2012-04-03 23:25:05

emphire
Member
From: Canada
Registered: 2007-03-21
Posts: 203

Re: RC Script for SSH tunnel

Autosshd is an options for this too.

Offline

Board footer

Powered by FluxBB