Skip to content

Instantly share code, notes, and snippets.

@t2psyto
Created November 6, 2012 12:15
Show Gist options
  • Select an option

  • Save t2psyto/4024312 to your computer and use it in GitHub Desktop.

Select an option

Save t2psyto/4024312 to your computer and use it in GitHub Desktop.
mac address maintainer for WZR-G144N, AP-50W, WLA2-G54C
# -*- coding: cp932 -*-
import mechanize as mc
import time
import sgmllib
import re
class Stripper(sgmllib.SGMLParser):
def __init__(self):
sgmllib.SGMLParser.__init__(self)
def strip(self, some_html):
self.theString = ""
self.feed(some_html)
self.close()
return self.theString
def handle_data(self, data):
self.theString += data
stripper = Stripper()
class accesspoint()
DEBUG = False
br = None
maclist = None
user = None
pw = None
ip = None
aptype = None
def __init__(self,ip,user=None,pw=None):
self.login(ip,user,pw)
def login(self,ip,user=None,pw=None):
self.ip = ip
self.user = user
self.pw = pw
self.br = mc.Browser()
host = "http://" + ip
if self.DEBUG==True:
self.br.set_debug_redirects(True)
self.br.set_debug_responses(True)
self.br.set_debug_http(True)
if user != None:
self.br.add_password(host,user,pw)
self.br.set_handle_robots(False)
self.br.open(host)
def getmaclist(self):
pass
def delete(self,mac):
pass
def add(self,mac):
pass
class WZRG144N(accesspoint):
"""
aptype = WZR-G144N
"""
aptype = "WZR-G144N"
def getmaclist2(self):
r = self.br.open("http://" + self.ip + "/cgi-bin/cgi?req=frm&frm=mac.html").read()
r2 = stripper.strip(r.decode("euc-jp")).strip()
pat = re.compile("([0-9|A-F][0-9|A-F]:[0-9|A-F][0-9|A-F]:[0-9|A-F][0-9|A-F]:[0-9|A-F][0-9|A-F]:[0-9|A-F][0-9|A-F]:[0-9|A-F][0-9|A-F])\n ([◯|×])".decode("cp932"))
maclist = pat.findall(r2)
return maclist
def getmaclist(self):
r = self.br.open("http://"+ self.ip +"/cgi-bin/cgi?req=frm&frm=mac_reg.html").read()
r2 = stripper.strip(r.decode("euc-jp")).strip()
r2 = r2.split("検出された無線パソコン一覧".decode("cp932"))[0]
pat = re.compile("([0-9|A-F][0-9|A-F]:[0-9|A-F][0-9|A-F]:[0-9|A-F][0-9|A-F]:[0-9|A-F][0-9|A-F]:[0-9|A-F][0-9|A-F]:[0-9|A-F][0-9|A-F])".decode("cp932"))
self.maclist = pat.findall(r2)
return self.maclist
def add(self,mac):
target = mac.upper()
maclist_before = self.getmaclist()
if target in set(maclist_before):
print "this MAC address is already appended."
return True
if maclist_before.__len__() >= 64:
print "maclist is full."
return False
self.br.select_form(nr=0)
self.br.form["maclist"] = target
r = self.br.submit(name="ADD")
time.sleep(5)
maclist_before = self.getmaclist()
return True
def delete(self,mac):
target = mac.upper()
index = None
maclist_before = self.getmaclist()
for i, v in enumerate(maclist_before):
if v == target:
index = i
if index != None:
strindex = "DELETE" + str(index)
self.br.select_form(nr=0)
r = self.br.submit(name=strindex)
time.sleep(5)
maclist_after = self.getmaclist()
if len(maclist_before) - len(maclist_after) == 1:
return True
else:
return False
else:
print "macaddress is not found in maclist."
return None
class WLA2G54C(accesspoint):
"""
aptype = WLA2-G54C
"""
br = None
maclist = None
user = None
pw = None
ip = None
aptype = "WLA2-G54C"
def getmaclist(self):
r = self.br.open("http://"+ self.ip +"/kinou-mac-entry.htm").read()
r2 = stripper.strip(r.decode("euc-jp",'ignore')).strip()
r2 = r2.split("検出された無線パソコン一覧".decode("cp932"))[0]
pat = re.compile("([0-9|A-F][0-9|A-F]:[0-9|A-F][0-9|A-F]:[0-9|A-F][0-9|A-F]:[0-9|A-F][0-9|A-F]:[0-9|A-F][0-9|A-F]:[0-9|A-F][0-9|A-F])".decode("cp932"))
self.maclist = pat.findall(r2)
return self.maclist
def add(self,mac):
target = mac.upper()
maclist_before = self.getmaclist()
if target in set(maclist_before):
print "this MAC address is already appended."
return True
if maclist_before.__len__() >= 64:
print "maclist is full."
return False
self.br.select_form(nr=0)
self.br.form["wl_limit_mac"] = target
r = self.br.submit()
self.br.select_form(nr=2)
r = self.br.submit()
time.sleep(5)
maclist_before = self.getmaclist()
return True
def delete(self,mac):
target = mac.upper()
index = None
self.br.open("http://"+self.ip+"/advance/advance-lan-mac.htm")
maclist_before = self.getmaclist()
for i, v in enumerate(maclist_before):
if v == target:
index = i
if index != None:
strindex = "del" + str(index)
self.br.select_form(nr=1)
self.br.find_control(name=strindex).items[0].selected = True
req = ap.br.form.click()
# machanize がsubmitの最初のボタンを選んでしまうので対策。
# submitの最初のボタンによって付加されたパラメータを除去する。
req.data = req.data.replace("wl_mac_edit0=%CA%D4%BD%B8&","")
# BASIC認証でPOSTするときはヘッダに認証情報を直接書く。
# なぜかadd_password()が効かない。
#self.br.add_password("http://"+self.ip,self.user,self.pw)
req.add_header('Authorization', 'Basic %s' % base64.encodestring('%s:%s' % (self.user, self.pw)))
self.br.open(req)
time.sleep(1)
self.br.select_form(nr=2)
r = self.br.submit()
time.sleep(5)
maclist_after = self.getmaclist()
if len(maclist_before) - len(maclist_after) == 1:
return True
else:
return False
else:
print "macaddress is not found in maclist."
return None
class AP50W(accesspoint):
"""
aptype = AP-50W
"""
aptype = "AP-50W"
def getmaclist(self):
r = self.br.open("http://"+ self.ip +"/qr_sec.cgi").read()
r2 = stripper.strip(r.decode("cp932")).strip()
pat = re.compile("([0-9|A-F][0-9|A-F]-[0-9|A-F][0-9|A-F]-[0-9|A-F][0-9|A-F]-[0-9|A-F][0-9|A-F]-[0-9|A-F][0-9|A-F]-[0-9|A-F][0-9|A-F])".decode("cp932"))
temp = pat.findall(r2)
self.maclist = sorted(set(temp), key=temp.index)
return self.maclist
def add(self,mac):
target = mac.upper().replace(":","-")
maclist_before = self.getmaclist()
if target in set(maclist_before):
print "this MAC address is already appended."
return True
#if maclist_before.__len__() >= 64:
# print "maclist is full."
# return False
self.br.select_form(nr=1)
self.br.form["wl mac set macid"] = target
r = self.br.submit()
time.sleep(1)
maclist_after = self.getmaclist()
return True
def delete(self,mac):
target = mac.upper().replace(":","-")
index = None
maclist_before = self.getmaclist()
for i, v in enumerate(maclist_before, 1):
if v == target:
index = i
if index != None:
strindex = "mac delete%02d" % index
self.br.select_form(nr=2)
r = self.br.submit(name=strindex)
time.sleep(1)
maclist_after = self.getmaclist()
if len(maclist_before) - len(maclist_after) == 1:
return True
else:
return False
else:
print "macaddress is not found in maclist."
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment