gemini ssh script


#!/bin/sh

IPADDR=<VPS IP>

while true; do
	EXISTING_REMOTE_PID=`ssh root@$IPADDR lsof -i :1966 | grep sshd | awk '{print $2}' | uniq`

	if test $EXISTING_REMOTE_PID -gt 0 2> /dev/null; then
		echo "Existing SSH running port 1966 found (pid: $EXISTING_REMOTE_PID)! Attempting to terminate it..."
		ssh root@$IPADDR kill $EXISTING_REMOTE_PID
	else
		echo $(date)
		echo "No existing/old sessions found, attempting to connect..."
	fi

	ssh -R 1966:localhost:1980 -o ServerAliveInterval=15 -o ServerAliveCountMax=99999 -o ExitOnForwardFailure=yes gemini@$IPADDR -nNT
	
	echo "Connection Lost or Process Killed"
	echo "Date and time is  $(date)"
	sleep 1
done