使用python连接到wi-fi

使用python连接到wi-fi,python,cmd,Python,Cmd,想象一下,您有Windows7的“passwords.txt”文件,其中包含2000个Wifi“MyHomeWifi”密码,但其中只有一个是正确的。还有python。当然,问题是如何连接到这个wifi。 我只知道,要连接到wifi,您可以在命令行中使用: netsh-wlan-connect\u-Wifi\u-name\u from subprocess import check_output output = check_output('netsh wlan connect _Wifi_nam

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

netsh-wlan-connect\u-Wifi\u-name\u

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

print output
剩下的就看你了。 考虑合并如下(你必须做一些工作自己的人…):< /P> 实用地通过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()
更受控制的版本

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()