Created
December 3, 2010 17:24
-
-
Save 140am/727247 to your computer and use it in GitHub Desktop.
monitors an VPN connection and controls OSX Shimo.app in case of problems
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/python | |
# OSX VPN monitor | |
# | |
# requires: | |
# - http://www.chungwasoft.com/shimo/ | |
# - http://growl.info/ | |
# - http://appscript.sourceforge.net/ | |
import sys | |
import socket | |
import appscript | |
import time | |
MONITOR_HOST = 'DESTINATION_IP' | |
VPN_PROFILE = 'test - mobile' # as named in Shimo perferences | |
growl = appscript.app('GrowlHelperApp') | |
growl.register( | |
as_application='vpn check', | |
all_notifications=['vpn down', 'vpn up'], | |
default_notifications=['vpn down'] | |
) | |
vpn = appscript.app('Shimo') | |
def growl_alert(msg): | |
growl.notify( | |
with_name='vpn down', | |
title='VPN down', | |
description=msg, | |
application_name = 'vpn check' | |
) | |
if __name__ == '__main__': | |
print '\rvpn check initalizing..', | |
while True: | |
# up to 3 attempts to conenct to server with 5 sec timeout | |
# otherwise reconnect vpn connection and alert via growl | |
error_count = 3 | |
while error_count >= 1: | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.settimeout(5) | |
try: | |
s.connect((MONITOR_HOST, 53)) | |
except: | |
print '\rVPN DOWN - %s' % time.ctime() | |
error_count -= 1 | |
else: | |
print '\rVPN OK - %s' % time.ctime(), | |
error_count = 3 | |
time.sleep(5) | |
s.close() | |
sys.stdout.flush() | |
# ensure that overal internet is still up to avoid loop with | |
# 3 connection attempts | |
test_count = 3 | |
test_timeout = 5 | |
while test_count >= 1: | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.settimeout(test_timeout) | |
try: | |
s.connect((socket.gethostbyname('www.google.com'), 80)) | |
except: | |
print '\rNo Connection - %s' % time.ctime(), | |
sys.stdout.flush() | |
test_count = 3 | |
else: | |
time.sleep(1) | |
test_count -= 1 | |
# only called if all connect attempts failed | |
print 'reconnect VPN with 10 sec delay' | |
sys.stdout.flush() | |
# re connect vpn with 10 sec delay | |
vpn.disconnect() | |
time.sleep(10) | |
vpn.connect( | |
with_profile=VPN_PROFILE | |
) | |
growl_alert('VPN re-connected %s - %s' % (VPN_PROFILE, time.ctime())) | |
# delay before monitoring starts after connection | |
time.sleep(30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment