List All Wireless Networks Python for PC
Asked Answered
E

3

9

I am trying to find out how I can list all of the available wireless networks in Python. I am using Windows 8.1.

Is there a built-in function I can call, or through a library?

Please kindly show me the code which prints the list.

Eurythmics answered 7/8, 2015 at 1:48 Comment(1)
Check the scan() function in this file: github.com/changyuheng/pywinwifi/blob/master/src/winwifi/…Osmanli
C
20

You'll want the subprocess module and a windows command:

import subprocess
results = subprocess.check_output(["netsh", "wlan", "show", "network"])

A little extra to just get SSID's.

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)

https://docs.python.org/2/library/subprocess.html

Crepuscule answered 7/8, 2015 at 1:58 Comment(5)
["netsh", "wlan", "show", "all"] gives more information, including the Access Point MAC Adresses (BSSIDD), if anyone needs those.Unfix
@Unfix i tryed with "all" but fails with: 'ascii' codec can't decode byte 0xc3 in position 5996: ordinal not in range(128)Flew
@Flew You might try decoding the text as Unicode: results = results.decode("utf8")Crepuscule
@Crepuscule i am getting this error FileNotFoundError: [Errno 2] No such file or directory: 'netsh': 'netsh', i am using linuxBackfire
@Backfire This answer is specifically for windows, as that is what OP was asking about. For Linux, you can do a similar thing by calling subprocess.check_output() with the proper command for your distro to list networks. You will have to parse the output differently as it will not be formatted the same as the windows output.Crepuscule
M
9

This is a question from the olden days, but even at the time the accepted answer could have been much more Pythonic - e.g.,

r = subprocess.run(["netsh", "wlan", "show", "network"], capture_output=True, text=True).stdout
ls = r.split("\n")
ssids = [k for k in ls if 'SSID' in k]

If you just want to SSID names in a list, change the ssids = line to

ssids = [v.strip() for k,v in (p.split(':') for p in ls if 'SSID' in p)]
Mover answered 26/2, 2020 at 1:8 Comment(1)
Works perfectly. However, Windows sometimes needs to be kicked to show SSID that was recently activated. Any idea how to do that? I am doing that manually by clicking on network icon on the Windows task bar. :-(Kussell
K
-4

c:\netsh

C:\netsh\wlan

c:\netsh\wlan)Show all

Kasher answered 21/1, 2019 at 23:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.