If you are speaking of a VPN used to access a private network, perhaps the easiest approach would be to check if an IP address specific to that network is reachable.
For example, if your VPN connects you to a network 192.168.100.0/24, using ping
to check if 192.168.100.1 (probably the gateway) is reachable:
if ping -c 4 192.168.100.1 &> /dev/null
then
echo "connected"
else
echo "not connected"
fi
Solution copied from this answer (in my example, -c 4
tries 4 times).
If you are using a VPN to avoid blocking, you might instead test against any IP address that is otherwise blocked.
This approach is not very sophisticated, but its advantage is its simplicity, and that it should work independent from the type of VPN and also on most operating systems.
utunX
)to route traffic through. Which interface does your VPN use?networksetup -listpppoeservices
used to work for me but it suddenly stopped listing my VPN configuration. I even recreated the VPN configuration under network settings but it's still not listed. I'm not sure if this is deliberate or a bug in macOS 13.2.1.