Forked from 2called-chaos/setup-autossh-tunnel.sh
Last active
September 6, 2022 00:17
-
-
Save ziogaschr/74884b8d5095c86a7cef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# credits to: https://gist.github.com/2called-chaos/4285767 | |
# Install: | |
# curl -O https://gist.githubusercontent.com/ziogaschr/74884b8d5095c86a7cef/raw/d236c87d89a34b588f37838843279e8e02f073e9/setup-autossh-tunnel.sh | |
# chmod u+x setup-autossh-tunnel.sh | |
# ./setup-autossh-tunnel.sh | |
# | |
# Extra: | |
# it is good to make a new user on both host and remote (http://linuxaria.com/howto/permanent-ssh-tunnels-with-autossh) | |
# useradd -m -s /bin/false autossh | |
SSH_USER="autossh" | |
SSH_SERVER="example.com" | |
SSH_PORT="3000" | |
LOCAL_USER="autossh" | |
LOCAL_DAEMON_USER="root" | |
LOCAL_KEYFILE="/home/$LOCAL_USER/.ssh/id_rsa" | |
LOCAL_PORT="22" | |
REMOTE_PORT="4444" | |
# 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="tunnel_rpi" | |
SERVICE_DESC="AutoSSH-RPi" # 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 | |
echo "Please accept the key once" | |
su -s /bin/sh $LOCAL_USER -c "ssh -i $LOCAL_KEYFILE -p $SSH_PORT $SSH_USER@$SSH_SERVER & echo SSH works" | |
set +e | |
echo "Start writing the file" | |
# 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 $SERVICE_DESC | |
# Description: Creates a SSH Tunnel for $SERVICE_DESC | |
### END INIT INFO | |
# | |
export PATH="\${PATH:+\$PATH:}/usr/sbin:/sbin" | |
# config | |
USER="$LOCAL_DAEMON_USER" | |
PIDFILE="$SERVICE_PIDFILE" | |
# TUNNEL="-N -R $REMOTE_PORT:127.0.0.1:$LOCAL_PORT -i $LOCAL_KEYFILE -p $SSH_PORT $SSH_USER@$SSH_SERVER"; | |
TUNNEL_ARGS=( -N | |
-o ServerAliveInterval=60 | |
-o ServerAliveCountMax=3 | |
-o BatchMode=yes | |
-o StrictHostKeyChecking=no | |
-R $REMOTE_PORT:127.0.0.1:$LOCAL_PORT | |
-i $LOCAL_KEYFILE | |
-p $SSH_PORT | |
$SSH_USER@$SSH_SERVER | |
) | |
DAEMON="/usr/bin/autossh" | |
DAEMON_OPTS="-M $CONTROL_PORT ${TUNNEL_ARGS[@]}" | |
# autossh config | |
export AUTOSSH_GATETIME=20 | |
export AUTOSSH_POLL=60 | |
export AUTOSSH_FIRST_POLL=30 | |
export AUTOSSH_DEBUG=1 | |
export AUTOSSH_LOGFILE=/var/log/$SERVICE_SCRIPT | |
# 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 --name "autossh" --make-pidfile --pidfile \$PIDFILE --start --background --exec \$DAEMON -- \$DAEMON_OPTS | |
} | |
do_stop() { | |
echo "Stopping: $SERVICE_DESC (daemon)" | |
start-stop-daemon -u \$USER --name "autossh" --pidfile \$PIDFILE --stop --retry 30 | |
} | |
do_restart() { | |
echo "Restarting: $SERVICE_DESC (daemon)" | |
do_stop | |
sleep 5 | |
do_start | |
} | |
do_status() { | |
echo "Check Status: $SERVICE_DESC (daemon)" | |
if start-stop-daemon -u \$USER --pidfile \$PIDFILE --status; then | |
echo "Running" | |
else | |
echo "Stopped" | |
exit 1 | |
fi | |
} | |
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 | |
;; | |
status) | |
do_status | |
;; | |
uninstall) | |
uninstall_script | |
;; | |
*) | |
echo "Usage: "\$1" {start|stop|restart|status|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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I suspect you might be missing an ampersand?
su -s /bin/sh $LOCAL_USER -c "ssh -i $LOCAL_KEYFILE -p $SSH_PORT $SSH_USER@$SSH_SERVER & echo SSH works"
perhaps should be:
su -s /bin/sh $LOCAL_USER -c "ssh -i $LOCAL_KEYFILE -p $SSH_PORT $SSH_USER@$SSH_SERVER && echo SSH works"