ddwrt_config.py
Go to the documentation of this file.
00001 
00002 import os, sys
00003 import urllib2, urllib
00004 import base64
00005 import StringIO
00006 import string
00007 import csv
00008 
00009 class DDWRT:
00010     def __init__(self, hostname, username, password):
00011         self.hostname = hostname
00012         self.username = username
00013         self.password = password
00014 
00015     def fetch(self, url, args=None):
00016         req = urllib2.Request(url)
00017         base64string = base64.encodestring("%s:%s" % (self.username, self.password))[:-1]
00018 
00019         req.add_header("Authorization", "Basic %s" % base64string)
00020         if args:
00021             req.data = urllib.urlencode(args)
00022 
00023         fp = urllib2.urlopen(req)
00024         body = fp.read()
00025         fp.close()
00026 
00027         return body
00028 
00029     def connect(self, essid):
00030         url = "http://%s/apply.cgi" % self.hostname
00031 
00032         args = {}
00033         args["submit_button"]="Wireless_Basic"
00034         args["action"]="ApplyTake"
00035         args["change_action"]="gozila_cgi"
00036         args["submit_type"]="save"
00037         args["wl0_nctrlsb"]=""
00038         args["wl1_nctrlsb"]=""
00039         args["iface"]=""
00040         args["wl0_mode"]="sta"
00041         args["wl0_net_mode"]="n-only"
00042         args["wl0_ssid"]=essid
00043         args["wl0_distance"]="500"
00044         args["eth1_bridged"]="1"
00045         args["eth1_multicast"]="0"
00046         args["eth1_nat"]="1"
00047         args["eth1_ipaddr"]="4"
00048         args["eth1_ipaddr_0"]="0"
00049         args["eth1_ipaddr_1"]="0"
00050         args["eth1_ipaddr_2"]="0"
00051         args["eth1_ipaddr_3"]="0"
00052         args["eth1_netmask"]="4"
00053         args["eth1_netmask_0"]="0"
00054         args["eth1_netmask_1"]="0"
00055         args["eth1_netmask_2"]="0"
00056         args["eth1_netmask_3"]="0"
00057         args["wl1_mode"]="ap"
00058         args["wl1_net_mode"]="disabled"
00059         args["wl1_ssid"]="dd-wrt"
00060         args["wl1_closed"]="0"
00061         args["wl1_distance"]="2000"
00062         args["eth2_bridged"]="1"
00063         args["eth2_multicast"]="0"
00064         args["eth2_nat"]="1"
00065         args["eth2_ipaddr"]="4"
00066         args["eth2_ipaddr_0"]="0"
00067         args["eth2_ipaddr_1"]="0"
00068         args["eth2_ipaddr_2"]="0"
00069         args["eth2_ipaddr_3"]="0"
00070         args["eth2_netmask"]="4"
00071         args["eth2_netmask_0"]="0"
00072         args["eth2_netmask_1"]="0"
00073         args["eth2_netmask_2"]="0"
00074         args["eth2_netmask_3"]="0"
00075 
00076         body = self.fetch(url, args)
00077 
00078     def current_ap(self):
00079         url = "http://%s/Status_Wireless.live.asp" % self.hostname
00080         body = self.fetch(url)
00081         
00082         line = None
00083         lines = body.split("\n")
00084 
00085         d = {}
00086         for line in lines:
00087           line = line[1:-1]
00088           line = line.replace(" ", "")
00089           parts = line.split("::", 1)
00090           if len(parts) == 2:
00091             d[parts[0]] = parts[1]
00092 
00093         essid = d.get('wl_ssid', '')
00094         wl_channel = d.get('wl_channel', '').split()[0]
00095         channel = int(wl_channel)
00096         rate = d.get('wl_rate', '')
00097 
00098         signal = None
00099         noise = None
00100         snr = None
00101         quality = None
00102 
00103         tx_power = d.get('wl_xmit', '')
00104 
00105         active_wireless = d.get('active_wireless', None)
00106         if active_wireless:
00107           active_wireless = active_wireless.replace("'", "")
00108           parts = active_wireless.split(",")
00109           macaddr = parts[0]
00110           interface = parts[1]
00111           if len(parts) == 7:
00112             signal = int(parts[4])
00113             noise = int(parts[5])
00114             snr = int(parts[6])
00115             quality = signal * 1.24 + 116
00116           else:
00117             signal = int(parts[5])
00118             noise = int(parts[6])
00119             snr = int(parts[7])
00120             quality = int(parts[8])/10
00121 
00122         print essid
00123 
00124     def site_survey(self):
00125         url = "http://%s/Site_Survey.asp" % self.hostname
00126 
00127         body = self.fetch(url)
00128 
00129         lines = body.split("\n")
00130         for i in range(len(lines)):
00131           if lines[i].startswith("var table = "):
00132             break
00133 
00134         aplines = []
00135         for j in range(i+1, len(lines)):
00136           if lines[j].startswith(");"): break
00137           line = lines[j].strip()
00138           if not line: continue
00139           if line[0] == ",": line = line[1:]
00140 
00141           aplines.append(line)
00142 
00143         aps = {}
00144         
00145         fp = StringIO.StringIO(string.join(aplines, '\n'))
00146         reader = csv.reader(fp)
00147         for row in reader:
00148           essid = row[0]
00149           macattr = row[2]
00150           channel = int(row[3])
00151           rssi = int(row[4])
00152           noise = int(row[5])
00153           beacon = int(row[6])
00154 
00155           aps[essid] = row
00156 
00157         for ap, row in aps.items():
00158             print ap, row[8][:20]
00159 
00160 import getopt
00161 def main(argv, stdout, environ):
00162     progname = sys.argv[0]
00163     optlist, args = getopt.getopt(argv[1:], "", ["help"])
00164     
00165     dd = DDWRT("192.168.1.1", "root", "admin")
00166 
00167     if len(args) == 0:
00168         dd.current_ap()
00169         return
00170         
00171     cmd = args[0]
00172     if cmd == "connect":
00173         dd.connect(args[1])
00174     elif cmd == "survey":
00175         dd.site_survey()
00176 
00177 if __name__ == "__main__":
00178     main(sys.argv, sys.stdout, os.environ)
00179 


wifi_ddwrt
Author(s): Scott Hassan , Eitan Marder-Eppstein
autogenerated on Thu Jun 6 2019 20:52:59