For this purpose we will use subprocess module which is available in (shipped with) python.
First import the library.
import subprocess
Then lets scan for all available wireless networks (make sure you have enabled wifi in your pc/laptop)
scan_results = subprocess.check_output(["netsh", "wlan", "show", "network"])
Now let’s dig deeper into the results. For python 3, first decode the thing into ascii.
scan_results = scan_results.decode("ascii") scan_results = scan_results.replace("\r","")
Now print the scan_results variable.
For further filtering the result lets add the following code snippet.
lst = scan_results.split("\n") lst = lst[4:]
if len(lst)>0: for eachItem in lst: if eachItem[0:4] == "SSID": SSID_cleared = eachItem[eachItem.find(":")+2:] print (SSID_cleared)
What we get from the scan is something like “SSID1 : Linoroid, SSID2 : Public_Wifi” etc. So what we do here is, finding the “:” using find command. It will return an index number. ” +2 ” means the position from the “:”. You can do your own experiments regarding this.
Following is the entire code.
import subprocess
scan_results = subprocess.check_output(["netsh", "wlan", "show", "network"])
scan_results = scan_results.decode("ascii") # this is for python 3 scan_results = scan_results.replace("\r","")
lst = scan_results.split("\n") lst = lst[4:]
if len(lst)>0: for eachItem in lst: if eachItem[0:4] == "SSID": SSID_cleared = eachItem[eachItem.find(":")+2:] print (SSID_cleared)
Get SSIDs of Available WiFi Networks