opexxx
5/22/2015 - 7:42 AM

ssh_tunnel

#!/bin/sh
# Simple init script to dig a ssh tunnel
# Author: Christoph Heer <christoph.heer@googlemail.com>

SSH_ARGS="user@remote.host -p 22 -N -n -L 4950:localhost:4949"
PID_FILE="/var/run/remote_host_tunnel.pid"

case "$1" in
start)
    if [ -f $PID_FILE ]; then
        echo "ssh tunnel already exists"
    else
        echo "dig ssh tunnel..."
        start-stop-daemon --start --exec /usr/bin/ssh --background --pidfile=$PID_FILE --make-pidfile -- $SSH_ARGS
    fi
;;

stop)
    echo "break down ssh tunnel..."
    start-stop-daemon --stop --pidfile=$PID_FILE
    rm $PID_FILE
;;

restart)
    $0 stop
    $0 start
;;

*)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac