ToughGuy
2021-08-30 11:32:14 +08:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
import subprocess as sp
device = 'en0'
interval = 2
networks = {
'SSID1': 'LOCATION',
'SSID2': 'LOCATION',
}
def send_notify(title, content):
sp.check_output(['osascript', '-e', 'display notification "{}" with title "{}"'.format(title, content)])
def get_ssid():
cmd = ['networksetup', '-getairportnetwork', device]
out = sp.check_output(cmd).decode().strip()
if not out:
return
fields = out.split()
if len(fields) == 4:
return fields[-1]
return None
def get_location():
return sp.check_output(['networksetup', '-getcurrentlocation']).decode().strip()
def set_location(location):
# send_notify('网络位置发生变化', '当前位置: {}'.format(location))
sp.check_output(['networksetup', '-switchtolocation', location])
def main():
while 1:
ssid = get_ssid()
location = networks.get(ssid, 'Automatic')
if get_location() != location:
set_location(location)
time.sleep(interval)
if __name__ == '__main__':
try:
main()
except Exception:
pass