使用Python获取WiFi配置文件列表

时间:2018-08-31 11:09:48

标签: python python-3.x batch-file cmd wifi

我正在尝试收集已安装的WiFi配置文件的列表。我已经为此使用了一批,效果很好。我正在尝试使其与python一起使用,以将网络生成为列表,供其他功能使用。我现在在Python脚本中拥有的是:

subprocess.call(["for", "/f", "\"tokens=2*delims=:\"", "%a", "in", "(', "netsh", "wlan", "show", "profiles')", "do", "(if", "\"%a\"", "neq", "\"\"", "echo(%a)"])

我使用print()来确保它正确地进入了cmd,并且它给了我:

for /f "tokens=2*delims=:" %a in ('netsh wlan show profiles') do (if "%a" neq "" echo(%a)

上一个批处理文件为:

@echo off
set "flag="
(for /f "tokens=1*delims=:" %%a in ('netsh wlan show profiles') do (
    if "%%a"=="User profiles" set flag=true
    if defined flag if "%%~b" neq "" (
        for /f "tokens=*" %%c in ("%%~b") do echo(%%c
    )
))>out.txt
type out.txt

我收到的错误是:

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1702, in __call__ return self.func(*args) File "C:\Users\Documents\Test.py", line 83, in WirelessFiltering subprocess.call(["for", "/f", "\"tokens=2*delims=:\"", "%a", "in", "('netsh", "wlan", "show", "profiles')", "do", "(if", "\"%a\"", "neq", "\"\"", "echo(%a)"]) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 304, in call with Popen(*popenargs, **kwargs) as p: File "C:\Users\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 756, in __init__ restore_signals, start_new_session) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1155, in _execute_child startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

尝试一下,请告诉我它是否能为您带来想要的结果。

import subprocess

a = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8').split('\n')
a = [i.split(":")[1][1:-1] for i in a if "All User Profile" in i]
for i in a:
    results = subprocess.check_output(['netsh', 'wlan', 'show', 'profile']).split('\n')
    try:
        print ("{}".format(i, results[0]))
    except IndexError:
        print format(i)

它的功能与批处理文件几乎相同,只是有所不同,在这里我只获得了包含All User Profiles的行,如果没有它,它仍然会获得配置文件,但显然也包含不需要的行。

正如您在评论中提到的那样,您想对远程计算机执行此操作,您需要将远程交换机与域凭据合并。.类似:

subprocess.check_output(['netsh', '-r', 'device', '-u ', 'domain\user', '-p', 'password', 'wlan', 'show', 'profiles']