使用python连接到wi-fi

Pep*_*nds 3 python cmd

想象一下,你有Windows 7,文件"passwords.txt",其中包含2000个Wifi"MyHomeWifi"密码,但其中只有一个是正确的.你有python.当然,问题是如何连接到这个wifi.我只知道,要连接到wifi,您可以在命令行中使用:

netsh wlan connect _Wifi_name_

Tor*_*xed 6

from subprocess import check_output
output = check_output('netsh wlan connect _Wifi_name_', shell=True)

print output
Run Code Online (Sandbox Code Playgroud)

剩下的由你决定.考虑合并以下(你必须自己做一些工作...):

with open('mypasswords.txt') as fh:
    for line in fh:
        ...
Run Code Online (Sandbox Code Playgroud)

务实地"写"密码作为输入通过python而不是作为参数传递:

from subprocess import Popen, STDOUT, PIPE
from time import sleep

handle = Popen('netsh wlan connect _Wifi_name_', shell=True, stdout=PIPE, stderr=STDOUT, stdin=PIPE)

sleep(5) # wait for the password prompt to occur (if there is one, i'm on Linux and sudo will always ask me for a password so i'm just assuming windows isn't retarded).
handle.stdint.write('mySecretP@ssW0rd\n')
while handle.poll() == None:
    print handle.stdout.readline().strip()
Run Code Online (Sandbox Code Playgroud)

一个更受控制的版本.