Added all the files

This commit is contained in:
Gordon Grant-Stuart 2022-11-22 15:18:19 +00:00
parent 79f2597690
commit a1458a66fb
3 changed files with 88 additions and 2 deletions

View File

@ -1,3 +1,8 @@
# TunnelKeeper
# tunnelkeeper
### TunnelKeeper keeps SSH tunnels open
TunnelKeeper keeps SSH tunnels open
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`.

9
etc/tunnels.conf.example Normal file
View File

@ -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

72
tunnelkeeper Normal file
View File

@ -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