I want to improve the script above to make it "force" direct connections.
Another issue with this script is that its pinging only once and if that ping fails, it stops and then starts the service.
I think it would be much better if the script pings 10 times, and if 10 out of 10 fails, it will restart the service.
This would increase the reliability of the script and also in the same time, make connections leave the relay and connect directly.
But I'm failing to do so, any ideas to improve the code with the insights above in mind ?
Edit:
I think I got it..
1- It will ping "headquarters" 10 times using tailscale.
This will help connections through tailscale prefer "direct" instead of relay.
2- If at least one of the tailscale ping works, it won't do anything.
This will avoid the service to being brought down every time.
3- If all pings fails, it will restart the tailscale service.
#!/bin/sh
DEST="headquarters"
SUCCESS=0
COUNT=0
while [ $COUNT -le 9 ]
do
for DEST in $DEST
do
COUNT=`expr $COUNT + 1`
tailscale ping --c 1 -timeout 1s $DEST >/dev/null 2>/dev/null
# ping -c 1 -t 100 $DEST
if [ $? -eq 0 ]
then
SUCCESS=`expr $SUCCESS + 1`
fi
done
done
if [ $SUCCESS -ge 1 ] && [ $COUNT -eq 10 ]
then
exit 0
else
/usr/local/sbin/pfSsh.php playback svc stop tailscale
sleep 5
/usr/local/sbin/pfSsh.php playback svc start tailscale
sleep 5
exit 1
fi
done
One important observation is, if there are more peers in the tailscale network, you can and should add them to this script.
See, if you are only pinging one host, if that host goes down, the script will take the entire tailscale service down affecting other hosts.
Code for multiple hosts
#!/bin/sh
DEST="server-1"
DEST1="server-2"
DEST2="servier-3"
SUCCESS=0
COUNT=0
while [ $COUNT -le 9 ]
do
for DEST in $DEST
do
COUNT=`expr $COUNT + 1`
tailscale ping --c 1 --timeout 1s $DEST >/dev/null 2>/dev/null
# ping -c 1 -t 100 $DEST
if [ $? -eq 0 ]
then
SUCCESS=`expr $SUCCESS + 1`
fi
tailscale ping --c 1 --timeout 1s $DEST1 >/dev/null 2>/dev/null
# ping -c 1 -t 100 $DEST1
if [ $? -eq 0 ]
then
SUCCESS=`expr $SUCCESS + 1`
fi
tailscale ping --c 1 --timeout 1s $DEST2 >/dev/null 2>/dev/null
# ping -c 1 -t 100 $DEST2
if [ $? -eq 0 ]
then
SUCCESS=`expr $SUCCESS + 1`
fi
done
done
if [ $SUCCESS -ge 1 ] && [ $COUNT -eq 10 ]
then
exit 0
else
/usr/local/sbin/pfSsh.php playback svc stop tailscale
sleep 5
/usr/local/sbin/pfSsh.php playback svc start tailscale
sleep 5
exit 1
fi
done
The code above will sum SUCCESS variable, and if any of the hosts answers, tailscale service will be considered to be UP and no actions will be taken.