7 proc = subprocess.Popen([
'networksetup',
'-listallhardwareports'], stdout=subprocess.PIPE)
11 line = proc.stdout.readline()
14 words = line.rstrip().split()
15 if (2 < len(words)
and 16 words[0] ==
'Hardware' and words[1] ==
'Port:' and words[2] ==
'Wi-Fi'):
18 elif wifi
and words[0] ==
'Device:':
19 device_name = words[1]
22 if device_name ==
None:
23 raise Exception(
'Wi-Fi device not found')
27 proc = subprocess.Popen([
'ifconfig', device_name], stdout=subprocess.PIPE)
31 line = proc.stdout.readline()
34 words = line.rstrip().split()
35 if words[0] ==
'status:':
38 raise Exception(
'Unknown Wi-Fi status')
41 def set_power(power, device_name = get_device_name()):
42 proc = subprocess.check_output([
'networksetup',
'-setairportpower', device_name, power])
45 proc = subprocess.Popen([
'networksetup',
'-getairportnetwork', device_name],
46 stdout=subprocess.PIPE)
49 line = proc.stdout.readline()
52 words = line.rstrip().split()
53 if (4 <= len(words)
and 54 words[0] ==
'Current' and words[1] ==
'Wi-Fi' and words[2] ==
'Network:'):
57 raise Exception(
'Wi-Fi not connected')
64 if prev_status != status:
67 if status !=
'active':
73 if __name__ ==
'__main__':
74 print(
'device name = %s' % device_name())
75 print(
'status = %s' % status())
78 if status() ==
'active':
79 print(
'connected to %s' % ssid())
def set_power(power, device_name=get_device_name())
def get_status(device_name=get_device_name())
def get_ssid(device_name=get_device_name())