From a1458a66fb54e0c236b762e91cf149c782afe78a Mon Sep 17 00:00:00 2001 From: Gordon Grant-Stuart Date: Tue, 22 Nov 2022 15:18:19 +0000 Subject: [PATCH] Added all the files --- README.md | 9 +++-- etc/tunnels.conf.example | 9 +++++ tunnelkeeper | 72 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 etc/tunnels.conf.example create mode 100644 tunnelkeeper diff --git a/README.md b/README.md index 8676ef9..e039864 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ -# TunnelKeeper +# tunnelkeeper +### TunnelKeeper keeps SSH tunnels open -TunnelKeeper keeps SSH tunnels open \ No newline at end of file +Install it as a service with `tunnelkeeper install`. + +Create a config file `/opt/tunnelkeeper/etc/tunnels.conf`. It's an ssh config file, so see `man ssh_config` for information. TunnelKeeper will connect to each host listed, and make sure every connection in `tunnels.conf` stays open in the background. + +If you make changes to tunnels.conf, run `systemctl restart tunnelkeeper`. diff --git a/etc/tunnels.conf.example b/etc/tunnels.conf.example new file mode 100644 index 0000000..ef2f325 --- /dev/null +++ b/etc/tunnels.conf.example @@ -0,0 +1,9 @@ +Host server1 + Hostname 123.45.67.89 + User ubuntu + LocalForward 10001 127.0.0.1:22 + +Host server1 + Hostname 123.45.67.90 + User ubuntu + LocalForward 10002 127.0.0.1:22 diff --git a/tunnelkeeper b/tunnelkeeper new file mode 100644 index 0000000..141adba --- /dev/null +++ b/tunnelkeeper @@ -0,0 +1,72 @@ +#!/usr/bin/env bash + +REALPATH="$(realpath $0)" +BASEDIR="${REALPATH%/*}" + +mkdir -p "$BASEDIR/var" +mkdir -p "$BASEDIR/etc" +CONFFILE="$BASEDIR/etc/tunnels.conf" + +if [[ ! -f "$CONFFILE" ]]; then + echo "Config file \"$CONFFILE\" does not exist" + exit 1 +fi + +case "$1" in + FORKSTART ) + touch "$BASEDIR/var/${2}.connected" + while [[ -e "$BASEDIR/var/${2}.connected" ]]; do + ssh -F $CONFFILE -N $2 &> /dev/null + sleep 5 + done + ;; + FORKKILL ) + kill $(sudo netstat -tnlp | grep "127.0.0.1:${2}" | grep -o '[0-9]*/ssh' | grep -o '[0-9]*') &>/dev/null + ;; + FORKDEL ) + rm "$BASEDIR/var/${2}.connected" + ;; + start) + if [[ -e "$BASEDIR/var/tunnelkeeper.pid" ]]; then + exit + fi + echo $$ > "$BASEDIR/var/tunnelkeeper.pid" + cat $CONFFILE | grep '^Host ' | sed 's/^Host //' | xargs -I% -P0 $0 FORKSTART % &>/dev/null & + ;; + stop) + rm "$BASEDIR/var/tunnelkeeper.pid" + cat $CONFFILE | grep '^Host ' | sed 's/^Host //' | xargs -I% -P0 $0 FORKDEL % + cat $CONFFILE | grep -o 'LocalForward [0-9]* ' | grep -o '[0-9]*' | xargs -I% -P0 $0 FORKKILL % &>/dev/null + ;; + install ) + if [[ $UID -ne 0 ]]; then + echo "You must be root to do this" + exit + fi + mkdir -p /opt/tunnelkeeper/var + mkdir -p /opt/tunnelkeeper/etc + if [[ -f "$BASEDIR/etc/tunnels.conf" ]]; then + cp "$BASEDIR/etc/tunnels.conf" /opt/tunnelkeeper/etc + fi + cp "$REALPATH" "/opt/tunnelkeeper/tunnelkeeper.sh" + echo "[Unit] +Description=TunnelKeeper keeps SSH tunnels open. +After=network.target +[Service] +User=root +Group=root +Type=forking +ExecStart=/opt/tunnelkeeper/tunnelkeeper.sh start +ExecStop=/opt/tunnelkeeper/tunnelkeeper.sh stop +RestartSec=15 +Restart=always +[Install] +WantedBy=multi-user.target" >> /lib/systemd/system/tunnelkeeper.service + systemctl daemon-reload + systemctl enable tunnelkeeper.service + echo "tunnelkeeper service installed" + ;; + * ) + echo -e "\nUsage: $(basename $0) [start|stop|install]\n" + ;; +esac