Skip to content

Instantly share code, notes, and snippets.

@opragel
Created June 25, 2017 23:38
Show Gist options
  • Save opragel/80ee6a1ee0fcd5f4cc00ae04dd65163f to your computer and use it in GitHub Desktop.
Save opragel/80ee6a1ee0fcd5f4cc00ae04dd65163f to your computer and use it in GitHub Desktop.
is_pritunl_connected.py
#!/usr/bin/python
# Shout to @nmcspadden for the all the original/good code in this
import platform
"""
is_vpn_connected()
checks to see if any interface is conected to an internal IP
"""
def is_vpn_connected():
"""
is_vpn_connected()
checks to see if any interface is conected to an internal IP
"""
if platform.system() == 'Darwin':
try:
# This will only work on OS X
from SystemConfiguration import (
SCDynamicStoreCopyValue,
SCDynamicStoreCreate
)
# Load up the System Configuration dynamic store
net_config = SCDynamicStoreCreate(None, "net", None, None)
# Query the AnyConnect DNS dictionary
vpn = SCDynamicStoreCopyValue(
net_config, "State:/Network/Service/Pritunl/DNS"
)
# 'vpn' is empty if AnyConnect is not currently active
if vpn is None:
return False
return True
except ImportError:
# If the dynamic store check fails, fall back to the 'utun' method
pass
def main():
vpn_status = is_vpn_connected()
print "Is VPN Connected: %s" % vpn_status
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment