From 255040e00933e8f6e3110dbfe16ae56aac7c8c12 Mon Sep 17 00:00:00 2001 From: Gordon Grant-Stuart Date: Mon, 5 Dec 2022 21:15:19 +0000 Subject: [PATCH] ditto --- .gitignore | 1 + etc/tunnelkeeper.conf | 15 +++++++++++++++ etc/tunnels.conf.example | 15 --------------- tunnelkeeper | 29 ++++++++++++++++------------- 4 files changed, 32 insertions(+), 28 deletions(-) create mode 100644 .gitignore create mode 100644 etc/tunnelkeeper.conf delete mode 100644 etc/tunnels.conf.example diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..90565d4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +var/* \ No newline at end of file diff --git a/etc/tunnelkeeper.conf b/etc/tunnelkeeper.conf new file mode 100644 index 0000000..91f4e09 --- /dev/null +++ b/etc/tunnelkeeper.conf @@ -0,0 +1,15 @@ +[tunnelkeeper] +debug 0 + +[ssh] # Everything below here obeys the same rules as ~/.ssh/config + +# Examples +# 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/etc/tunnels.conf.example b/etc/tunnels.conf.example deleted file mode 100644 index 014e6c0..0000000 --- a/etc/tunnels.conf.example +++ /dev/null @@ -1,15 +0,0 @@ -# -# TunnelKeeper config file -# -#: debug 0 -# - -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 index 7f9fbee..dcbf1a4 100755 --- a/tunnelkeeper +++ b/tunnelkeeper @@ -3,12 +3,16 @@ REALPATH="$(realpath $0)" BASEDIR="${REALPATH%/*}" -mkdir -p "$BASEDIR/var" -mkdir -p "$BASEDIR/etc" -CONFFILE="$BASEDIR/etc/tunnels.conf" +rm $BASEDIR/var/*.conf &>/dev/null # Create separate .conf files in var/ from etc/tunnelkeeper.conf +awk -v "dir=$BASEDIR/var" ' + /^\[/ {sec=$1; gsub("[\[\]]","",sec)} + !/^\[/ {print >> dir"/"sec".conf"}' < $BASEDIR/etc/tunnelkeeper.conf -if [[ ! -f "$CONFFILE" ]]; then - echo "Config file \"$CONFFILE\" does not exist" +SSHCONF="$BASEDIR/var/ssh.conf" +TKCONF="$BASEDIR/var/tunnelkeeper.conf" + +if [[ ! -f "$SSHCONF" ]]; then + echo "Config file \"$SSHCONF\" does not exist" exit 1 fi @@ -19,7 +23,7 @@ function ruroot () { fi } -DEBUGLEVEL=$(cat "$CONFFILE" | grep -i "#: *debug" | grep -o [0-9]) +DEBUGLEVEL=$(cat $TKCONF | grep -i "debug" | grep -o [0-9]) case "$DEBUGLEVEL" in 2) dbgopt='-v';; 3) dbgopt='-vvv';; @@ -36,7 +40,7 @@ case "$1" in FORKSTART ) touch "$BASEDIR/var/${2}.connected" while [[ -e "$BASEDIR/var/${2}.connected" ]]; do - ssh -F "${CONFFILE}" $dbgopt -N $2 2>&1 | dbg + ssh -F "${SSHCONF}" $dbgopt -N $2 2>&1 | dbg sleep 5 done ;; @@ -45,12 +49,12 @@ case "$1" in start) [[ -e "$BASEDIR/var/tunnelkeeper.pid" ]] && exit echo $$ > "$BASEDIR/var/tunnelkeeper.pid" - cat "$CONFFILE" | awk '/^Host / {print $2}' | xargs -I% -P0 $0 FORKSTART % & + cat "$SSHCONF" | awk '/^Host / {print $2}' | xargs -I% -P0 $0 FORKSTART % & ;; stop) rm "$BASEDIR/var/tunnelkeeper.pid" - cat "$CONFFILE" | awk '/^Host / {print $2}' | sed 's/^Host //' | xargs -I% -P0 $0 FORKDEL % - cat "$CONFFILE" | grep -o 'LocalForward [0-9]* ' | grep -o '[0-9]*' | xargs -I% -P0 $0 FORKKILL % + cat "$SSHCONF" | awk '/^Host / {print $2}' | sed 's/^Host //' | xargs -I% -P0 $0 FORKDEL % + cat "$SSHCONF" | grep -o 'LocalForward [0-9]* ' | grep -o '[0-9]*' | xargs -I% -P0 $0 FORKKILL % ;; restart) $0 stop; sleep 2; $0 start ;; install ) @@ -58,8 +62,7 @@ case "$1" in mkdir -p /opt/tunnelkeeper/var mkdir -p /opt/tunnelkeeper/etc if [[ "$BASEDIR" != '/opt/tunnelkeeper/' ]]; then - cp -n "$BASEDIR/etc/tunnels.conf" /opt/tunnelkeeper/etc - cp "$BASEDIR/etc/tunnels.conf.example" /opt/tunnelkeeper/etc + cp -n "$BASEDIR/etc/tunnelkeeper.conf" /opt/tunnelkeeper/etc/ cp "$REALPATH" "/opt/tunnelkeeper/tunnelkeeper" fi ln -f -s /opt/tunnelkeeper/tunnelkeeper /usr/local/bin/tunnelkeeper @@ -96,7 +99,7 @@ WantedBy=multi-user.target" > /lib/systemd/system/tunnelkeeper.service ;; config ) ruroot - vi "$CONFFILE" + vi "$BASEDIR/etc/tunnelkeeper.conf" ;; * ) echo -e "\nUsage: $(basename $0) start|stop|restart|install|uninstall|config|list\n"