Search code examples
pythonpython-3.xwifi

How to get all available SSID names in python without "SSID:"?


Ok so my code is

results = subprocess.check_output(["netsh", "wlan", "show", "network"])
results = results.decode("ascii") # needed in python 3
results = results.replace("\r","")
ls = results.split("\n")
ls = ls[4:]
ssids = []
x = 0
while x < len(ls):
    if x % 5 == 0:
        ssids.append(ls[x])
    x += 1
print(ssids)

How could I get the output of the variable "ssids" without it saying "SSID (number of SSID) : " before each item?


Solution

  • Running this locally using Windows yielded a results list containing the following example elements:

    ['SSID 1 : WYZE_D73596CE021C547F', 'SSID 2 : FiOS-ZLGT4-5G', ...]

    Consequently, if you wanted to modify the list of SSIDs, you could do the following:

    ssids = [ssid.split(':',1)[-1].strip() for ssid in ssids]

    This is a list comprehension which iterates over the existing SSIDs, splits once on a colon and takes the last element, and strips the resulting string (i.e. removes whitespace on either end), and will assign the new list to ssids:

    ['WYZE_D73596CE021C547F', 'FiOS-ZLGT4-5G', ...]

    Alternatively, you could iterate over the existing list and print each element if you did not want to modify SSIDs:

    for ssid in ssids:
        print(f'{ssid.split(":")[-1].strip()}')