A VPN monitoring Script

From Da Nerd Mage Wiki
Revision as of 13:06, 21 September 2025 by Tinker (talk | contribs) (→‎Running it periodically in the background)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Proven on:
Logo Debian.png
13 (trixie)

OpenVPN with PIA seems to occasionally forget to act like a VPN

When this happens, you may just want to know about it & remind them by restarting OpenVPN

Finding out if you're still protected:

Compare your public IP to that of the rest of the network

wget http://ipinfo.io/ip -qO -

&

ssh user@othermachine 'wget http://ipinfo.io/ip -qO -'

user & othermachine are a valid user on another machine on your LAN. One that is NOT running a VPN connection.)

NOTE: The assumption made here is that you have configured this machine to use passwordless SSH when connecting to othermachine.

Here's a nifty little shell script...

#!/bin/bash

if nc -zw1 google.com 443
then
  echo "We have Internets..."
else
  echo "NOPE!  No Internets..."
  exit
fi

Local=$(wget http://ipinfo.io/ip -qO -)

# This requires that 'ssh-copy-id user@othermachine' has been done
Remote=$(ssh user@othermachine wget http://ipinfo.io/ip -qO -)

# Required because the VPN bypasses local DNS
Automation=192.168.9.0

echo Local: $Local
echo Remote: $Remote
echo Automation: $Automation

if [ $Local != $Remote ]
then
  echo "PIA is currently behaving"
  echo $(date) " - PIA is Just Fine!" >> vpnfix.log
  mosquitto_pub -h "$Automation" -t "Scotts_Speaker_Medium" -m "P I A is OK"
 else
  echo "PIA is mis-behaving"
  date >> vpnfix.log
  echo $(date) " - PIA is Buggered!" >> vpnfix.log
  mosquitto_pub -h "$Automation" -t "Scotts_Speaker_Medium" -m "P I A is Boogered"

  echo "Attempting repair..."
  sudo service openvpn start
fi

192.168.9.0 happens to be the IP address of an MQTT broker on my LAN...

Not only does it check if PIA is doing its thing, it announces the result verbally (the mosquitto... lines) and restarts openvpn (asking for a password for sudo...). It could use a bit of improvement, but it works.

NOTE: You have to ssh into othermachine manually first so the machine knows it's ok.

NOTE: Ya kinda need to install mosquitto-clientsif you want the verbal notifications and set up a Node-Red flow to transfer the messages to your voice assistant or it ain't gonna work quite right...

I have a GH mini at my desk & a node-Red flow that monitors for MQTT messages with the topic Scotts_Speaker_Medium, then passes the message on through TTS to that device.

(Of course, if you don't want the voice assistant part, just comment out the 2 mosquitto_pub lines...)

Running it periodically in the background

  • As root: configure passwordless SSH to user@thermachine
  • Move it to /usr/local/bin
    • sudo mv vpnchk /usr/local/bin
  • Pair down the logging & noise so it just lets you know when there's an issue
    • comment out the logging and MQTT lines under "PIA is currently behaving"
    • change the logfile from vpnfix.log to /var/log/vpnfix.log under "PIA is mis-behaving"
  • Create the cron job (I chose to run it every 5 minutes...)
    • sudo crontab -e
*/5 * * * * /usr/local/bin/vpnchk