Skip to content

Instantly share code, notes, and snippets.

@bobthecow
Forked from kennethreitz/gist:430908
Created June 9, 2010 02:26
Show Gist options
  • Save bobthecow/430956 to your computer and use it in GitHub Desktop.
Save bobthecow/430956 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
""" TapDat - Simple VPN Setup.
"""
import re
import sys
from commands import getoutput as cmd
def main():
"""Sets split-tunnled VPN access for NetApp"""
print 'TapDat: Simple VPN Setup'
print 'Picking up stones...'
old_gateway = get_gateway()
print 'Opening the floodgates...'
open_vpn()
set_gateway(old_gateway)
print 'Dat has been Tapt'
def get_gateway():
"""Returns existing default gateway"""
existing_route = cmd("route get default | grep 'gateway'")
reg = r'.*?((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?![\d])'
rg = re.compile(reg, re.IGNORECASE|re.DOTALL)
m = rg.search(existing_route)
return m.group(1)
def set_gateway(gate):
"""Sets system default to given gateway"""
print cmd("sudo route change default {0}".format(gate))
def open_vpn():
"""Opens VPN connection and adds appropriate routes"""
vpn_script = '''
tell application "System Events"
tell current location of network preferences
set VPNservice to service "NetApp" -- name of the VPN service
if exists VPNservice then connect VPNservice
end tell
end tell
'''
cmd("echo '{0}' | osascript".format(vpn_script))
raw_input('Enter when ready.')
cmd("sudo route -nv add -net 10 -interface utun0")
if __name__ == '__main__':
main()
@bobthecow
Copy link
Author

Oh, I don't actually need this implementation. I just filed it away as "maybe I'll base a VPN management script on this later" :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment