Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kennethreitz
Created June 9, 2010 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kennethreitz/430908 to your computer and use it in GitHub Desktop.
Save kennethreitz/430908 to your computer and use it in GitHub Desktop.
Quick and Dirty VPN Setup
#!/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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment