Created
June 26, 2015 11:17
-
-
Save tevino/54a0869152994179ae4d to your computer and use it in GitHub Desktop.
A script for automatically reconnecting VPN on OS X after connection dropped
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import re | |
from time import sleep | |
from subprocess import check_output as run | |
last_print = None | |
def printf(s, no_repeat=False): | |
global last_print | |
if no_repeat: | |
if last_print != s: | |
print s | |
else: | |
print s | |
last_print = s | |
def get_current_vpn(): | |
out = run(["/usr/sbin/scutil", "--nc", "list"]) | |
match = re.search(r'\(Connect.+"(.+)"', out, re.IGNORECASE) | |
if match: | |
return match.group(1) | |
def closed_manually(): | |
out = run(['/usr/bin/tail', '/var/log/ppp.log']) | |
return '[TERMINATE]' in out | |
def connect(vpn): | |
return run(['/usr/sbin/scutil', '--nc', 'start', vpn]) | |
def main(): | |
last_vpn = None | |
while True: | |
vpn = get_current_vpn() | |
if not vpn: | |
if not last_vpn: | |
printf('No VPN connection...', True) | |
elif closed_manually(): | |
print 'Closed Manually: {}'.format(last_vpn) | |
last_vpn = None | |
else: | |
print 'Connection lost, reconnecting...' | |
connect(last_vpn) | |
else: | |
printf('Monitoring {}'.format(vpn), True) | |
last_vpn = vpn or last_vpn | |
sleep(1) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🙌