Skip to content

Instantly share code, notes, and snippets.

@geekdadley
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save geekdadley/23302038557bb3c73627 to your computer and use it in GitHub Desktop.

Select an option

Save geekdadley/23302038557bb3c73627 to your computer and use it in GitHub Desktop.
Service config of auto start a ssh tunnel
#!/bin/sh
# chkconfig: 2345 20 80
# description: auto build ssh tunnel towards aliyun.wangnan.me
# Source function library.
if [ -f /etc/init.d/functions ] ; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
. /etc/rc.d/init.d/functions
else
exit 1
fi
# Avoid using root's TMPDIR
unset TMPDIR
# Source networking configuration.
. /etc/sysconfig/network
if [ -f /etc/sysconfig/samba ]; then
. /etc/sysconfig/samba
fi
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 1
# Check that smb.conf exists.
[ -f /etc/samba/smb.conf ] || exit 6
SCREENNAME=aliyuntunnel
TUNNELCOMMAND="ssh -R 2233:localhost:22 tunnel@aliyun.wangnan.me"
result(){
if [ $1 -eq 0 ];then
echo -e "[\e[1;32mOK\e[0m]"
else
echo -e "[\e[1;32mOK\e[0m]"
fi
}
start() {
echo -n "Starting aliyuntunnel ..."
screen -dmS $SCREENNAME $TUNNELCOMMAND
result $?
}
stop() {
echo -n "Stoping aliyuntunnel ..."
screen -X -S $SCREENNAME quit
result $?
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
screen -ls
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 2
esac
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment