ddwrt_config.py
Go to the documentation of this file.
1 
2 import os, sys
3 import urllib2, urllib
4 import base64
5 import StringIO
6 import string
7 import csv
8 
9 class DDWRT:
10  def __init__(self, hostname, username, password):
11  self.hostname = hostname
12  self.username = username
13  self.password = password
14 
15  def fetch(self, url, args=None):
16  req = urllib2.Request(url)
17  base64string = base64.encodestring("%s:%s" % (self.username, self.password))[:-1]
18 
19  req.add_header("Authorization", "Basic %s" % base64string)
20  if args:
21  req.data = urllib.urlencode(args)
22 
23  fp = urllib2.urlopen(req)
24  body = fp.read()
25  fp.close()
26 
27  return body
28 
29  def connect(self, essid):
30  url = "http://%s/apply.cgi" % self.hostname
31 
32  args = {}
33  args["submit_button"]="Wireless_Basic"
34  args["action"]="ApplyTake"
35  args["change_action"]="gozila_cgi"
36  args["submit_type"]="save"
37  args["wl0_nctrlsb"]=""
38  args["wl1_nctrlsb"]=""
39  args["iface"]=""
40  args["wl0_mode"]="sta"
41  args["wl0_net_mode"]="n-only"
42  args["wl0_ssid"]=essid
43  args["wl0_distance"]="500"
44  args["eth1_bridged"]="1"
45  args["eth1_multicast"]="0"
46  args["eth1_nat"]="1"
47  args["eth1_ipaddr"]="4"
48  args["eth1_ipaddr_0"]="0"
49  args["eth1_ipaddr_1"]="0"
50  args["eth1_ipaddr_2"]="0"
51  args["eth1_ipaddr_3"]="0"
52  args["eth1_netmask"]="4"
53  args["eth1_netmask_0"]="0"
54  args["eth1_netmask_1"]="0"
55  args["eth1_netmask_2"]="0"
56  args["eth1_netmask_3"]="0"
57  args["wl1_mode"]="ap"
58  args["wl1_net_mode"]="disabled"
59  args["wl1_ssid"]="dd-wrt"
60  args["wl1_closed"]="0"
61  args["wl1_distance"]="2000"
62  args["eth2_bridged"]="1"
63  args["eth2_multicast"]="0"
64  args["eth2_nat"]="1"
65  args["eth2_ipaddr"]="4"
66  args["eth2_ipaddr_0"]="0"
67  args["eth2_ipaddr_1"]="0"
68  args["eth2_ipaddr_2"]="0"
69  args["eth2_ipaddr_3"]="0"
70  args["eth2_netmask"]="4"
71  args["eth2_netmask_0"]="0"
72  args["eth2_netmask_1"]="0"
73  args["eth2_netmask_2"]="0"
74  args["eth2_netmask_3"]="0"
75 
76  body = self.fetch(url, args)
77 
78  def current_ap(self):
79  url = "http://%s/Status_Wireless.live.asp" % self.hostname
80  body = self.fetch(url)
81 
82  line = None
83  lines = body.split("\n")
84 
85  d = {}
86  for line in lines:
87  line = line[1:-1]
88  line = line.replace(" ", "")
89  parts = line.split("::", 1)
90  if len(parts) == 2:
91  d[parts[0]] = parts[1]
92 
93  essid = d.get('wl_ssid', '')
94  wl_channel = d.get('wl_channel', '').split()[0]
95  channel = int(wl_channel)
96  rate = d.get('wl_rate', '')
97 
98  signal = None
99  noise = None
100  snr = None
101  quality = None
102 
103  tx_power = d.get('wl_xmit', '')
104 
105  active_wireless = d.get('active_wireless', None)
106  if active_wireless:
107  active_wireless = active_wireless.replace("'", "")
108  parts = active_wireless.split(",")
109  macaddr = parts[0]
110  interface = parts[1]
111  if len(parts) == 7:
112  signal = int(parts[4])
113  noise = int(parts[5])
114  snr = int(parts[6])
115  quality = signal * 1.24 + 116
116  else:
117  signal = int(parts[5])
118  noise = int(parts[6])
119  snr = int(parts[7])
120  quality = int(parts[8])/10
121 
122  print essid
123 
124  def site_survey(self):
125  url = "http://%s/Site_Survey.asp" % self.hostname
126 
127  body = self.fetch(url)
128 
129  lines = body.split("\n")
130  for i in range(len(lines)):
131  if lines[i].startswith("var table = "):
132  break
133 
134  aplines = []
135  for j in range(i+1, len(lines)):
136  if lines[j].startswith(");"): break
137  line = lines[j].strip()
138  if not line: continue
139  if line[0] == ",": line = line[1:]
140 
141  aplines.append(line)
142 
143  aps = {}
144 
145  fp = StringIO.StringIO(string.join(aplines, '\n'))
146  reader = csv.reader(fp)
147  for row in reader:
148  essid = row[0]
149  macattr = row[2]
150  channel = int(row[3])
151  rssi = int(row[4])
152  noise = int(row[5])
153  beacon = int(row[6])
154 
155  aps[essid] = row
156 
157  for ap, row in aps.items():
158  print ap, row[8][:20]
159 
160 import getopt
161 def main(argv, stdout, environ):
162  progname = sys.argv[0]
163  optlist, args = getopt.getopt(argv[1:], "", ["help"])
164 
165  dd = DDWRT("192.168.1.1", "root", "admin")
166 
167  if len(args) == 0:
168  dd.current_ap()
169  return
170 
171  cmd = args[0]
172  if cmd == "connect":
173  dd.connect(args[1])
174  elif cmd == "survey":
175  dd.site_survey()
176 
177 if __name__ == "__main__":
178  main(sys.argv, sys.stdout, os.environ)
179 
def __init__(self, hostname, username, password)
Definition: ddwrt_config.py:10
def main(argv, stdout, environ)
def site_survey(self)
def fetch(self, url, args=None)
Definition: ddwrt_config.py:15
def current_ap(self)
Definition: ddwrt_config.py:78
def connect(self, essid)
Definition: ddwrt_config.py:29


wifi_ddwrt
Author(s): Scott Hassan , Eitan Marder-Eppstein
autogenerated on Wed May 12 2021 02:21:31