Skip to content

Instantly share code, notes, and snippets.

@alxrogan
Created October 4, 2011 14:54
Show Gist options
  • Select an option

  • Save alxrogan/1261835 to your computer and use it in GitHub Desktop.

Select an option

Save alxrogan/1261835 to your computer and use it in GitHub Desktop.
Script to automate creation of SSH tunnels, with init style invocation
#!/bin/sh
# chkconfig: 235 99 00
# description: Start, Stop, and Restart the SSH Tunnel
case "$1" in
'start')
ssh -Nf proxy
ps aexf | grep "ssh -Nf proxy" | awk '{print $1}' > $HOME/tmp/tunnel.pid
# pgrep -f proxy > $HOME/tmp/tunnel.pid
;;
'stop')
pid=`cat $HOME/tmp/tunnel.pid`
kill $pid
rm -f $HOME/tmp/tunnel.pid
;;
'restart')
$0 stop && $0 start
;;
'status')
if [ -s $HOME/tmp/tunnel.pid ]; then
pid=`cat $HOME/tmp/tunnel.pid`
if [ "$?" = "0" ]; then
echo "SSH Tunnel (pid $pid) is running"
else
echo "SSH Tunnel is stopped"
fi
else
echo "SSH Tunnel is stopped"
fi
;;
*)
echo "Usage: $0 { start | stop | restart | status }"
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment