Last active
August 29, 2015 14:20
-
-
Save sugiana/db1448f30bf4b9ab2592 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Web admin modem 3Com ADSL Wifi | |
import socket | |
socket.setdefaulttimeout(30) | |
import urllib | |
import urllib2 | |
import sys | |
from sgmllib import SGMLParser | |
import os | |
import mechanize | |
import re | |
from pprint import pprint | |
class Form(SGMLParser): | |
def __init__(self): | |
SGMLParser.__init__(self) | |
self.form = {} | |
self.inputs = {} | |
def feed(self, content): | |
SGMLParser.feed(self, content) | |
self.close() | |
def start_form(self, attrs): | |
for attr in attrs: | |
if attr[0] == 'action': | |
self.form['action'] = attr[1] | |
def start_input(self, attrs): | |
if len(attrs) > 0: | |
name = '' | |
value = '' | |
for attr in attrs: | |
if attr[0] == "name": | |
name = attr[1] | |
elif attr[0] == "value": | |
value = attr[1] | |
if name: | |
self.inputs[name] = value | |
class MacParser(SGMLParser): | |
def __init__(self): | |
SGMLParser.__init__(self) | |
self.daftar = [] | |
self.baris = [] | |
self.data = [] | |
self.enable_mac_filter_label = True | |
self.enable_mac_filter = False | |
def start_table(self, attrs): | |
pass | |
def end_table(self): | |
pass | |
def start_tr(self, attrs): | |
pass | |
def end_tr(self): | |
if self.baris: | |
self.daftar.append( self.baris ) | |
self.baris = [] | |
def start_td(self, attrs): | |
pass | |
def end_td(self): | |
if self.data: | |
self.baris.append(' '.join(self.data)) | |
self.data = [] | |
def handle_data(self, data): | |
data = data.strip() | |
if not data: | |
return | |
if data.find('Enable MAC Address Filtering') > -1: | |
self.enable_mac_filter_label = True | |
raise EnableMac | |
return | |
if self.enable_mac_filter_label: | |
if data.find('var iDOD =') > -1: | |
if data.find('var iDOD = 1') > -1: | |
self.enable_mac_filter = True | |
self.enable_mac_filter_label = False | |
def open_url(url=None, p=None, write_last_page=True): | |
if url: | |
url = BASE_URL + url | |
else: | |
url = BASE_URL | |
try: | |
if p: | |
data = urllib.urlencode(p) | |
req = mechanize.Request(url, data) | |
r = mechanize.urlopen(req) | |
else: | |
r = mechanize.urlopen(url) | |
except urllib2.URLError, err: | |
print(err) | |
return '' | |
content = r.read() | |
if write_last_page: | |
f = open(last_page_file, 'w') | |
f.write(content) | |
f.close() | |
return content | |
def out(message): | |
print(message) | |
sys.exit() | |
def logout(alasan='sudah selesai'): | |
msg = 'Logout karena {alasan}.'.format(alasan=alasan) | |
print(msg) | |
open_url('cgi-bin/logout.exe', write_last_page=False) | |
out('Program berakhir.') | |
############## | |
# Blok Utama # | |
############## | |
if sys.argv[1:] and os.path.exists(sys.argv[1]): | |
# offline | |
parser = Form() | |
parser.feed(open(sys.argv[1]).read()) | |
print(parser.inputs) | |
sys.exit() | |
cookies = mechanize.CookieJar() | |
opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cookies)) | |
opener.addheaders = [('User-Agent','Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/20061201 Firefox/2.0.0.3 (Ubuntu-feisty)')] | |
mechanize.install_opener(opener) | |
BASE_URL = 'http://192.168.1.1/' | |
password = 'FIXME' | |
last_page_file = '/tmp/3com-wifi-adsl.stm' | |
NAME2MACS = { | |
'Lenovo A390': 'D4223F2AC91B', | |
'ASUS': '54271EB94AEB', | |
'SPC': 'EE4A5B11129A', | |
'Advan': 'C87D6507313B', | |
'Lenovo P780': '70720D599CEA', | |
} | |
MAC2NAMES = {} | |
for name in NAME2MACS: | |
mac = NAME2MACS[name] | |
MAC2NAMES[mac] = name | |
print('Buka halaman utama') | |
content = open_url() | |
parser = Form() | |
parser.feed(content) | |
p = dict(parser.inputs) | |
p['pws'] = password | |
print('Login') | |
content = open_url('cgi-bin/login.exe', p) | |
content = open_url('menu_admin.stm') | |
parser = Form() | |
parser.feed(content) | |
if 'action' not in parser.form: | |
out('Login gagal') | |
if parser.form['action'] != '/cgi-bin/logout.exe': | |
print(parser.form) | |
out('Form LOGOUT tidak ditemukan.') | |
content = open_url('wireless_mac.stm') | |
s = content | |
while s.find('\\') > -1: | |
s = s.replace('\\', ' ') | |
parser = MacParser() | |
parser.feed(s) | |
if parser.enable_mac_filter: | |
print('Saat ini MAC filtering enabled') | |
else: | |
print('Saat ini MAC filtering disabled') | |
old_macs = [] | |
if parser.enable_mac_filter: | |
regex = re.compile('document.tF.MAC([0-9]*)_([0-9]*).value="([0-9A-F]*)";') | |
macs = {} | |
for line in content.splitlines(): | |
match = regex.search(line.strip()) | |
if not match: | |
continue | |
row, col, value = match.groups() | |
if row in macs: | |
macs[row] += value | |
else: | |
macs[row] = value | |
for row in macs: | |
mac = macs[row] | |
old_macs.append(mac) | |
name = MAC2NAMES[mac] | |
print(' ' + name) | |
if not sys.argv[1:]: | |
logout('tidak ada yang perlu dilakukan') | |
if 'mac-filter-disable' in sys.argv: | |
p = {'mac_fun': ''} | |
print('MAC filter akan di-disable') | |
elif 'mac-filter-enable' in sys.argv: | |
new_macs = [] | |
for name in sys.argv: | |
if name in NAME2MACS: | |
mac = NAME2MACS[name] | |
new_macs.append(mac) | |
if not new_macs: | |
names = ['"' + name + '"' for name in NAME2MACS.keys()] | |
msg = ' enable MAC Filtering tanpa MAC address satupun. '\ | |
'Pilihannya: {pilihan}' | |
msg = msg.format(pilihan=', '.join(names)) | |
logout(msg) | |
old_macs.sort() | |
new_macs.sort() | |
need_change = old_macs != new_macs | |
if not need_change: | |
logout('daftar MAC address yang akan di-filter masih sama') | |
print('MAC filter akan di-enable') | |
p = {'mac_fun': '1', | |
'mac_fil': '1', # deny mac list | |
} | |
enable_mac_filter = True | |
row = 0 | |
for mac in new_macs: | |
row += 1 | |
for col in range(6): | |
key = 'MAC{row}_{col}'.format(row=row, col=col) | |
i = col * 2 | |
val = mac[i:i+2] | |
p[key] = val | |
name = MAC2NAMES[mac] | |
if mac in old_macs: | |
print(' {name} memang sudah.'.format(name=name)) | |
else: | |
print(' {name} akan kena filter.'.format(name=name)) | |
print('DEBUG') | |
pprint(p) | |
content = open_url('cgi-bin/wireless_f.exe', p) | |
logout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment