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