How can I tell if OS X is connected to a VPN network from the command line?

How can I tell if OS X is connected to a VPN network from the command line?

By running ifconfig without any arguments when I am connected, I see that there is a utun0 interface which appears to be the VPN connection. When I disconnect it goes away.

I believe I could use something like this to check for the string utun0 and count the occurrences:

ifconfig | grep -c utun0

But is there an easier or more effective way to check for this? If utun0 is a device, or even a pseudo device, shouldn't I be able to check if it exists with something like:

if [ -a '/dev/utun0' ]

Unfortunately I don't see any changes in that directory when connecting and disconnecting, I just see /dev/tun0 through /dev/tun15 and I can't cat them even with sudo...

Is there a simpler way to tell if I have a VPN connection?


You can also, as of Mountain Lion1, use the scutil command.

For example:

$ scutil --nc list | grep Connected

For more detailed help, you can see the man page, or run:

$ scutil --nc help

Footnotes:

  1. I'm not aware of this command existing in versions of OSX previous to Mountain Lion, but I could be wrong.