Skip to content

Instantly share code, notes, and snippets.

@mfurlend
Last active December 7, 2015 16:42
Show Gist options
  • Save mfurlend/dd62e1a63943ccaaadea to your computer and use it in GitHub Desktop.
Save mfurlend/dd62e1a63943ccaaadea to your computer and use it in GitHub Desktop.
SSH tunnel to remote MySQL server. From localhost:3307 to root@dataserver:3306. Chkconfig compatible. (chkconfig --add datatunnel). Place script in /etc/init.d/dataserver
#! /bin/zsh
#
# Init file for dataserver SSH tunnel to dataserver's MySQL server
#
# chkconfig: 2345 95 5
# description: SSH tunnel to dataserver's MySQL server. Runs locally at port 3307.
#
# processname: data_tunnel
DATSRV=("${(@f)$(getent hosts dataserver 10.0.0.188 | uniq | awk '{print $1"\n"$2}')}")
case "$1" in
start)
DATA_TUNNEL=$(sudo lsof -i 4@$DATSRV[1] -i 4@$DATSRV[2] | grep ESTABLISHED | wc -l)
[[ ( $DATA_TUNNEL == 0 ) ]] && (print "No tunnel.. making one"; print "ssh -fNg -L 3307:127.0.0.1:3306 root@dataserver"; ssh -fNg -L 3307:127.0.0.1:3306 root@$DATSRV[1])
;;
stop)
kill -9 $(sudo lsof -i 4@$DATSRV[1] -i 4@$DATSRV[2] | awk '{print $2}' | grep -v PID)
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment