This commit is contained in:
Gordon Grant-Stuart 2023-04-12 12:18:12 +01:00
parent 67c62ebb95
commit 31a6e82325

View File

@ -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' &
;;