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:

Destination        Gateway            Flags        Refs      Use   Netif Expire
0/1               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 Netif contains 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              UGSc           15        0   utun1
def vpn_running?
  run_os_command('netstat -nr').split("\n").grep(/^0\/1.*utun1/).any?
  • The detection method is unreliable, depending on your definition of VPN, because on both of my Macs, there are 2 or 3 utun interfaces active without a user installed VPN. I believe that Back to My Mac and the carrier Wi-Fi calling features essentially set up VPNs to Apple and the carrier respectively.
    – user71659
    Oct 22, 2017 at 18:32
  • Thank you. I am resigned to not having a solution that is 100% accurate. I may continue to keep the existing test in place unless I can get a better one. Is there an attribute of the false positives you mentioned that could be used to filter them out? Oct 23, 2017 at 0:33

1 Answer 1


That's exactly what I have in my own code. In bash it looks like:

vpn=$( netstat -rn | grep utun1 | wc -l )
if [[ $vpn -eq 0 ]] ; then
    # no VPN active
    # VPN active

It's been pretty reliable in my environment. I'd like to be able to discern different VPN configurations in effect, but not so much as to have to code it (yet).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .