Skip to content

Instantly share code, notes, and snippets.

@2called-chaos
Last active April 30, 2018 15:38
Show Gist options
  • Save 2called-chaos/4285767 to your computer and use it in GitHub Desktop.
Save 2called-chaos/4285767 to your computer and use it in GitHub Desktop.
Adds a start script which will setup a port forwarding over SSH (via autossh) for database traffic tunneling. You can use this script for every port actually. I might called it different but I only need it for MySQL ;-)
#!/bin/bash
# Install:
# curl -O https://gist.githubusercontent.com/2called-chaos/4285767/raw/setup-autossh-tunnel.sh
# chmod u+x setup-autossh-tunnel.sh
# ./setup-autossh-tunnel.sh
SSH_USER="mysql_tunnel"
SSH_SERVER="db.example.net"
SSH_PORT="22"
LOCAL_USER="root"
LOCAL_KEYFILE="~/.ssh/id_rsa"
LOCAL_PORT="3306"
REMOTE_PORT="3306"
# Use a different port for every tunnel to the same machine!
# Be aware that the port above the declared one is also used (e.g.: 20009: 20009 & 20010)
CONTROL_PORT="20009"
SERVICE_SCRIPT="mysql_tunnel"
SERVICE_DESC="AutoSSH-MySQL" # just for display
SERVICE_PIDFILE="/var/run/$SERVICE_SCRIPT.pid"
###########
# install autossh
if [[ ! -x /usr/bin/autossh ]] ; then
read -p "You will need autossh! Shall I invoke 'aptitude install autossh' for you (Y/n)? "
if [ "$REPLY" != "n" ]; then
aptitude install autossh
fi
fi
# trigger ssh connect to accept key
set -e
su $LOCAL_USER -c "ssh -i $LOCAL_KEYFILE -p $SSH_PORT $SSH_USER@$SSH_SERVER echo SSH works"
set +e
# write init.d script
cat > /etc/init.d/$SERVICE_SCRIPT <<EOF
#!/bin/bash
#
### BEGIN INIT INFO
# Provides: $SERVICE_SCRIPT
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop a autossh MySQL tunnel
# Description: Creates a SSH Tunnel for MySQL
### END INIT INFO
#
export PATH="\${PATH:+\$PATH:}/usr/sbin:/sbin"
# config
USER="$LOCAL_USER"
PIDFILE="$SERVICE_PIDFILE"
TUNNEL="-N -L $LOCAL_PORT:127.0.0.1:$REMOTE_PORT -i $LOCAL_KEYFILE -p $SSH_PORT $SSH_USER@$SSH_SERVER";
DAEMON="/usr/bin/autossh"
DAEMON_OPTS="-M $CONTROL_PORT \$TUNNEL"
# autossh config
export AUTOSSH_GATETIME=30
export AUTOSSH_POLL=15
# exit if autossh is missing
if [[ ! -x /usr/bin/autossh ]]; then
echo "Please install autossh: aptitude install autossh"
exit 1
fi
do_start() {
echo "Starting: $SERVICE_DESC (daemon)"
start-stop-daemon -u \$USER --make-pidfile --pidfile \$PIDFILE --start --background --exec \$DAEMON -- \$DAEMON_OPTS
}
do_stop() {
echo "Stopping: $SERVICE_DESC (daemon)"
start-stop-daemon -u \$USER --pidfile \$PIDFILE --stop --retry 30
}
do_restart() {
echo "Restarting: $SERVICE_DESC (daemon)"
do_stop && do_start
}
uninstall_script() {
read -p "This will remove the init.d script! Continue (Y/n)? "
if [ "\$REPLY" == "n" ]; then
echo "Skipped delete!"
exit 1
else
do_stop
update-rc.d -f $SERVICE_SCRIPT remove
rm /etc/init.d/$SERVICE_SCRIPT
read -p "Remove autossh (y/N)? "
if [ "\$REPLY" == "y" ]; then
aptitude remove autossh
fi
fi
}
case "\$1" in
start)
do_start
;;
stop)
do_stop
;;
restart)
do_restart
;;
uninstall)
uninstall_script
;;
*)
echo "Usage: "\$1" {start|stop|restart|uninstall}"
exit 1
;;
esac
exit 0
EOF
# add init script
chmod 755 /etc/init.d/$SERVICE_SCRIPT
update-rc.d $SERVICE_SCRIPT defaults
echo "done"
exit 0
@johnjwatson
Copy link

Hello there: Your script looks great (am about to test it!), but I had a small question:
Where are the options: ServerAliveInterval and ServerAliveCountMax set for autossh?
In a nutshell, I am trying to figure out what the heartbeat parameters are for autossh - and how to see them. Thanks a tonne again for the awesome script!

@2called-chaos
Copy link
Author

@johnjwatson This script will create the init script which you may alter before or after executing the installation script (before by editing the script (note that some chars need to be escaped) or after by editing the script in (by default) /etc/init.d/mysql_tunnel)

There are a few AutoSSH settings defined here and you may add SSH options here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment