使用python连接到wi-fi

时间:2013-08-14 09:07:48

标签: python cmd

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

netsh wlan connect _Wifi_name_

1 个答案:

答案 0 :(得分:6)

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

print output

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

with open('mypasswords.txt') as fh:
    for line in fh:
        ...

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

更受控制的版本。

相关问题