will it work?

This commit is contained in:
Gordon Grant-Stuart 2022-12-05 16:47:09 +00:00
parent b3ac932e87
commit 20b04a72bc
2 changed files with 25 additions and 21 deletions

View File

@ -1,3 +1,9 @@
#
# TunnelKeeper config file
#
#: debug 0
#
Host server1
Hostname 123.45.67.89
User ubuntu

View File

@ -13,50 +13,48 @@ if [[ ! -f "$CONFFILE" ]]; then
fi
function ruroot () {
if [[ $UID -ne 0 ]]; then
if [[ $UID -ne 0 ]]; then
echo "You must be root to do this"
exit
fi
}
if [[ "${2}" == "--debug" ]]; then
dbg=$(tty) # Terminal for the forked processes to print debug info to.
echo "debug enabled on $dbg" > $dbg
else
dbg='/dev/null'
fi
DEBUGLEVEL=$(cat "$CONFFILE" | grep -i "#: *debug" | grep -o [0-9])
case "$DEBUGLEVEL" in
2) dbgopt='-v';;
3) dbgopt='-vvv';;
*) dbgopt='';;
esac
function dbg () {
if [[ $DEBUGLEVEL != "0" ]]; then
logger -t tunnelkeeper
fi
}
case "$1" in
FORKSTART )
[[ "$3" == "/dev/null" ]] && vvv='' || vvv='-vvv' # debug mode
touch "$BASEDIR/var/${2}.connected"
while [[ -e "$BASEDIR/var/${2}.connected" ]]; do
ssh -F "${CONFFILE}" $vvv -N $2 &> $3
ssh -F "${CONFFILE}" $dbgopt -N $2 2>&1 | dbg
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"
;;
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" | awk '/^Host / {print $2}' | xargs -I% -P0 $0 FORKSTART % $dbg &
cat "$CONFFILE" | 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 %
;;
restart)
$0 stop
$0 start
;;
restart) $0 stop; sleep 2; $0 start ;;
install )
ruroot
mkdir -p /opt/tunnelkeeper/var
@ -102,6 +100,6 @@ WantedBy=multi-user.target" > /lib/systemd/system/tunnelkeeper.service
vi "$CONFFILE"
;;
* )
echo -e "\nUsage: $(basename $0) start|stop|restart|install|uninstall|config|list [--debug]\n"
echo -e "\nUsage: $(basename $0) start|stop|restart|install|uninstall|config|list\n"
;;
esac