Skip to content

Instantly share code, notes, and snippets.

@KenoLeon
Created April 23, 2022 02:21
Show Gist options
  • Select an option

  • Save KenoLeon/53809c4997286df24ab0846c98b362b0 to your computer and use it in GitHub Desktop.

Select an option

Save KenoLeon/53809c4997286df24ab0846c98b362b0 to your computer and use it in GitHub Desktop.
get wifi data from subprocess and parse it
import subprocess
import re
# pipe stdout from subprocess to a string
process = subprocess.run(["airport", "en0", "--scan"],
check=True,
stdout=subprocess.PIPE,
universal_newlines=True)
# Split string by rows
processRows = process.stdout.split("\n")
# Make 2 empty lists for the network names and strengths
networklist = []
networkStrenght = []
# Parse by 2 or more spaces and build dicts:
for item in processRows:
splitData = re.split(r'\s{2,}', item)
if len(splitData) > 1:
networklist.append(splitData[1])
networkStrenght.append(splitData[2])
# Remove first item, which is the header
networklist.pop(0)
networkStrenght.pop(0)
# Utility function to join 2 lists into a dict:
def makeDict(list1, list2):
dict = {}
for i in range(len(list1)):
dict[list1[i]] = list2[i]
return dict
# Make a dict from the lists:
networks = makeDict(networklist, networkStrenght)
# Print the dict:
for key, value in networks.items():
print(key, value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment