Skip to content

Instantly share code, notes, and snippets.

@ZhanruiLiang
Created March 29, 2012 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZhanruiLiang/2238545 to your computer and use it in GitHub Desktop.
Save ZhanruiLiang/2238545 to your computer and use it in GitHub Desktop.
SYSU wifi autologon
import urllib2
from time import sleep
import os
import subprocess as sb
import re
User = 'your_netid'
Passwd = 'xxxxxxxxxx'
class AutoLogon:
def __init__(self, device='wlan0'):
self.device = device
self.connected = 0
self.cardTestPatt = re.compile('No such device', re.M)
self.dhcpFailPatt = re.compile('interface not found or invalid|time out', re.M)
self.dhcpSuccessPatt = re.compile(r'acknowledged (10\.\d+\.\d+\.\d+ from )', re.M)
@staticmethod
def _get_output(cmd):
p = sb.Popen(cmd, shell=True, stdout=sb.PIPE, stderr=sb.PIPE)
p.wait()
return p.stdout.read() + p.stderr.read()
def is_card_ready(self):
cmd = 'iwconfig %s' % self.device
output = self._get_output(cmd)
g = self.cardTestPatt.search(output)
if g:
return 0
return 1
def reload_driver(self):
os.system('sudo rmmod brcmsmac && sudo modprobe brcmsmac')
def dhcp(self):
output = self._get_output('pgrep -l dhcpcd')
if output:
pid = output.split()[0]
print 'dhcpcd exist, will kill it now.'
# os.system()
# killcmd = 'sudo dhcpcd -k'
killcmd = 'sudo kill %s' % pid
print self._get_output(killcmd)
cmd = 'sudo dhcpcd %s' % self.device
output = self._get_output(cmd)
# print output
if self.dhcpFailPatt.search(output):
return 0
elif self.dhcpSuccessPatt.search(output):
return 1
return 0
def get_state(self):
output = self._get_output('iwconfig %s' % self.device)
print '-'*80
print output
print '-'*80
def connect_adhoc(self):
cmd = 'sudo iwconfig %s essid "SYSUWLAN"' % self.device
output = self._get_output(cmd)
print output
def keep_alive(self):
try:
while 1:
urllib2.urlopen('http://10.10.2.22/portal/logon.cgi',
'PtUser=%s&PtPwd=%s&PtButton=Logon' % (User, Passwd))
if not self.connected:
print 'success'
self.connected = 1
sleep(10)
except urllib2.URLError as e:
print e
return 0
return 0
def start(self):
try:
if not self.is_card_ready():
self.reload_driver()
if not self.is_card_ready():
raise Exception('card not ready')
while 1:
self.connected = 0
self.connect_adhoc()
self.get_state()
for i in xrange(5):
if self.dhcp():
break
self.connect_adhoc()
sleep(2)
else:
raise Exception('dhcp failed')
self.get_state()
sleep(2)
if not self.keep_alive(): continue
except KeyboardInterrupt:
pass
if __name__ == '__main__':
AutoLogon().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment