#!/usr/bin/python3 import urllib.request import json import sys DEMO_ISPS = ('Riseup Networks', 'Leaseweb Deutschland GmbH') CI_LEAP_SE_ISPS = ('AS47172 Greenhost BV') url = 'https://ipapi.co/json' with urllib.request.urlopen(url) as _url: data = json.loads(_url.read().decode()) isp = data['org'] ip = data['ip'] print('ISP >> %s' % isp) print('IP >> %s' % ip) class BadCmd(Exception): pass try: cmd = sys.argv[1] if cmd == 'vpn_on': print('checking VPN ON...') assert isp in CI_LEAP_SE_ISPS elif cmd == 'vpn_off': print('checking VPN OFF...') assert isp not in CI_LEAP_SE_ISPS else: raise BadCmd() except BadCmd: print("Cannot parse that command. Valid commands: vpn_on, vpn_off") sys.exit(1) except Exception as exc: print("IP *NOT* as expected") print("%r" % exc) sys.exit(1) else: print("OK :)") sys.exit(0)