Skip to content

Instantly share code, notes, and snippets.

@keithweaver
Last active March 20, 2023 18:01
Show Gist options
  • Save keithweaver/00edf356e8194b89ed8d3b7bbead000c to your computer and use it in GitHub Desktop.
Save keithweaver/00edf356e8194b89ed8d3b7bbead000c to your computer and use it in GitHub Desktop.
Get Wifi information on Mac OSx using Python
# I tried to use the pip install wifi but it really didn't work.
# So created this
import subprocess
process = subprocess.Popen(['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport','-I'], stdout=subprocess.PIPE)
out, err = process.communicate()
process.wait()
print(out)
'''
Print out is:
agrCtlRSSI: -50
agrExtRSSI: 0
agrCtlNoise: -91
agrExtNoise: 0
state: running
op mode: station
lastTxRate: 243
maxRate: 300
lastAssocStatus: 0
802.11 auth: open
link auth: wpa2
BSSID: 24:de:c6:a6:9c:da
SSID: QueensuSecure_WPA2
MCS: 14
channel: 44,1
'''
# SSID is the wifi name
@fabioviegas
Copy link

Just a small suggestion: use the line below to print bytes with line break:

print("".join(map(chr, out)))

@warneat
Copy link

warneat commented May 24, 2021

Works fine, wonderful, thanks!
What i find quite useful by the way: Option-key + click on Wifi-Symbol gives you a lot information really quickly as well

@dmattera
Copy link

to expand on this, here's a function that will return all the output of the airport stdout into a python dictionary, allowing you easily extract the values independently:

def get_wifi_info():
	process = subprocess.Popen(['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport','-I'], stdout=subprocess.PIPE)
	out, err = process.communicate()
	process.wait()

	wifi_info = {}
	for line in out.decode("utf-8").split("\n"):
		if ": " in line:
			key, val = line.split(": ")
			key = key.replace(" ", "")
			val = val.strip()

			wifi_info[key] = val

	return wifi_info

wifi_info = get_wifi_info()
print(wifi_info["SSID"])

"""prints only the exact SSID string"""

@woshiluoyong
Copy link

Good! useful

@Alexandro1112
Copy link

Alexandro1112 commented Jan 24, 2023

Maybe make this:(if need get available wifi networks)

import subprocess


def get_list_wifi_networks():
     """ Function output all wi-fi networks,
           which available for your devise."""
     scan_cmd = subprocess.Popen(['airport', '-s'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
     scan_out, scan_err = scan_cmd.communicate()
     scan_out_data = dict()
     scan_out_lines = str(scan_out).split("\\n")[1:-1]
     for each_line in scan_out_lines:
          split_line = [e for e in each_line.split(" ") if e != ""]
          line_data = {"SSID": split_line[0], "RSSI": int(split_line[2]), "channel": split_line[3],
                       "HT": (split_line[4] == "Y"), "CC": split_line[5], "security": split_line[6]}
          scan_out_data[split_line[1]] = line_data
     return scan_out_data


for names in get_list_wifi_networks().values():
     i = list(names.values())[0]
     print(i.split())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment