I'd like to be able to programmatically detect the presence of a VPN on a Mac. Detecting if a VPN connection is active programmatically shows what the output of
netstat -nr will look like when a VPN is active:
Internet: Destination Gateway Flags Refs Use Netif Expire 0/1 10.81.10.5 UGSc 5 0 utun1 ...
Some of the information is significant for my purpose, the rest of it isn't. What exactly do I need to check? Only that
utun1? Or do I need to see
0/1 there too? Or if not, what then?
I am currently testing for the first and last data items in that line, and it seems to be working, but I'd like to reduce the probability of any false positives or negatives. In Ruby it is:
# This is determined by whether or not a line like the following # appears in the output of `netstat -nr`: # 0/1 10.137.0.41 UGSc 15 0 utun1 def vpn_running? run_os_command('netstat -nr').split("\n").grep(/^0\/1.*utun1/).any? end