#!/usr/bin/python3 import sh def getNetWorkServices(): x = sh.networksetup('-listallnetworkservices'); s = x.split('\n') s = [ i for i in s if i!='' ] s = s[1:] print(s); return s def getIpByService(s): print('s=', s) x = sh.networksetup('-getinfo', s) lines = x.split('\n') start = 'IP address: ' for l in lines: if l.startswith(start): return l[len(start):] return None def disableProxy(): services = getNetWorkServices() ret = {s:getIpByService(s) for s in services} for s,ip in ret.items(): if ip is not None: sh.networksetup('-setsocksfirewallproxystate', s, 'off') print(s, ip, 'socks stoped') def enableProxy(): services = getNetWorkServices() ret = {s:getIpByService(s) for s in services} for s,ip in ret.items(): if ip is not None: ip_domain = '.'.join(ip.split('.')[:-1]) sh.networksetup('-setsocksfirewallproxy', s, ip, '1086') sh.networksetup('-setproxybypassdomains', s, ip_domain) print(s, ip, 'socks started') if __name__ == '__main__': import sys if len(sys.argv) > 1 and sys.argv[1] == 'stop': disableProxy() else: enableProxy()