From 31a6e82325272f17572e02a2b73fb69a1d3f083e Mon Sep 17 00:00:00 2001 From: Gordon Grant-Stuart Date: Wed, 12 Apr 2023 12:18:12 +0100 Subject: [PATCH] wibble --- tunnelkeeper | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tunnelkeeper b/tunnelkeeper index f1688dd..d2869ad 100755 --- a/tunnelkeeper +++ b/tunnelkeeper @@ -1,6 +1,6 @@ #!/usr/bin/env bash -REALPATH="$(realpath $0)" +REALPATH="$(realpath "$0")" BASEDIR="${REALPATH%/*}" SSHCONF="$BASEDIR/var/ssh.conf" @@ -9,6 +9,7 @@ WATCHCONF="$BASEDIR/var/watch.conf" DEBUGCONF="$BASEDIR/var/debug.conf" function genconfig () { + mkdir -p "$BASEDIR/var" cat "$BASEDIR/etc/tunnelkeeper.conf" | grep -Eiv '^ *(watch|password|debug)' > "$SSHCONF" cat "$BASEDIR/etc/tunnelkeeper.conf" | grep -Ei '^( *watch|Host)' | awk '{print $1 " " $2}' | grep -i -B1 --no-group-separator watch | tr '\n' ' ' | sed 's/Host /\n/g; s/ *[Ww]atch//g' > "$WATCHCONF" cat "$BASEDIR/etc/tunnelkeeper.conf" | grep -Ei '^( *password|Host)' | awk '{print $1 " " $2}' | grep -i -B1 --no-group-separator password | tr '\n' ' ' | sed 's/Host /\n/g; s/ *[Pp]assword//g' > "$PWCONF" @@ -34,7 +35,7 @@ function dbg () { } function connect () { - ssh -F "${SSHCONF}" $(debugopt $1) -o "ControlMaster auto" -o "StrictHostKeyChecking no" -S "$BASEDIR/var/$1.tksock" -N $1 '#tunnelkeeper' 2>&1 | dbg $1 + ssh -F "${SSHCONF}" "$(debugopt $1)" -o "ControlMaster auto" -o "StrictHostKeyChecking no" -S "$BASEDIR/var/$1.tksock" -N $1 '#tunnelkeeper' 2>&1 | dbg $1 } @@ -60,14 +61,14 @@ case "$1" in fi exit ;; - FORKWATCH ) # makes sure the connection is still working, even if ssh doesn't drop it + FORKWATCH ) # makes sure the connection is still working, even if ssh doesn't drop it. Needs a login shell to work. while true; do TIMEOUT=$(awk "/^$2/ {print \$2}" ${WATCHCONF}) sleep $TIMEOUT echo "tick $2" | dbg if [[ $(timeout $TIMEOUT ssh localhost -o "StrictHostKeyChecking no" -S $BASEDIR/var/$2.tksock "echo tk") != "tk" ]]; then ssh localhost -O exit -S $BASEDIR/var/$2.tksock - echo "Killing connection to $2. Trying again." | dbg + echo "Killing connection to $2. Trying again." | dbg $2 fi done exit @@ -81,8 +82,8 @@ case "$1" in exit ;; start) - genconfig [[ -e "$BASEDIR/var/tunnelkeeper.pid" ]] && exit + genconfig echo $$ > "$BASEDIR/var/tunnelkeeper.pid" cat "$SSHCONF" | awk '/^Host / {print $2}' | xargs -I% -P0 $0 FORKSTART % '#tunnelkeeper' & ;;