1 |
#!/usr/bin/python |
2 |
|
3 |
import sys, os, re, pexpect, time, Cookie |
4 |
import Log, common |
5 |
|
6 |
try: |
7 |
from Config import libPaths, dbName, dbUser, dbPass, dbHost, dbPort |
8 |
except: |
9 |
Log.Message ("Error occured in finding basic configuration paramaters. Reverting to installer.", "index.py", "ERROR") |
10 |
import install |
11 |
sys.exit () |
12 |
|
13 |
for path in libPaths: sys.path.append (path) |
14 |
|
15 |
try: |
16 |
import pycisco, pyaaa |
17 |
except: |
18 |
Log.Message ("Couldn't import pycisco or pyaaa. Reverting to installer.", "index.py", "ERROR") |
19 |
import install |
20 |
sys.exit () |
21 |
|
22 |
import cgi, cgitb |
23 |
cgitb.enable () |
24 |
|
25 |
form = cgi.FieldStorage () |
26 |
|
27 |
defaultCursor = None |
28 |
def GetDefaultCursor (): |
29 |
global defaultCursor |
30 |
if defaultCursor == None: |
31 |
defaultCursor = pyaaa.SafeBase () |
32 |
defaultCursor._Resume (sessionID) |
33 |
return defaultCursor |
34 |
|
35 |
# Cookie stuff |
36 |
sessionID = None |
37 |
def ProcessCookie (): |
38 |
global sessionID |
39 |
cookie = Cookie.SimpleCookie (os.environ.get("HTTP_COOKIE", "")) |
40 |
if cookie.has_key ("sid"): |
41 |
sessionID = cookie["sid"].value |
42 |
try: |
43 |
GetDefaultCursor () |
44 |
except pyaaa.InvalidSession: |
45 |
# TODO: Remember when this happens to catch people trying to brute-force session IDs. |
46 |
Log.Message ("Invalid session ID: " + sessionID + ". Redirecting to login.", "index.py", "ERROR") |
47 |
print "Location: login.py" |
48 |
else: |
49 |
# Not logged in, fail. |
50 |
Log.Message ("User not logged in. Redirecting to login.", "index.py", "ERROR") |
51 |
print "Location: login.py" |
52 |
|
53 |
|
54 |
def GetSwitchList (): |
55 |
global switchList |
56 |
if switchList == []: |
57 |
switchList = GetDefaultCursor ()._GetRecords ("NetworkDevice") |
58 |
return switchList |
59 |
|
60 |
def ShowDefaultPage (): |
61 |
pageText = """ |
62 |
<p>Select a switch to get started!</p> |
63 |
<form action='index.py' method='get'> |
64 |
<select name='switch'> |
65 |
""" |
66 |
|
67 |
for switch in GetSwitchList (): |
68 |
pageText += "<option value='" + switch.split(".")[0] + "'>" + switch.split(".")[0] + "</option>" |
69 |
|
70 |
pageText += """ |
71 |
</select> |
72 |
<input type='hidden' name='action' value='status' /> |
73 |
<input type='submit' value='View Port Summary' /> |
74 |
</form> |
75 |
""" |
76 |
|
77 |
return pageText |
78 |
|
79 |
def ShowSwitchStatus (switchName): |
80 |
pageText = """ |
81 |
<h3>Port summary for %(switchName)s</h3> |
82 |
<script type='text/javascript'> |
83 |
$(document).ready (function () { |
84 |
$("a.portfast").click (function () { |
85 |
// Change the appearance and class so that it's no longer a "reveal"-style link |
86 |
$(this).attr ("class", "portfastload"); |
87 |
$(this).text ("Wait..."); |
88 |
|
89 |
portName = $(this).parents("tr").attr ('id'); |
90 |
$(this).load ("update.py?action=getportfast&switch=%(switchName)s&port=" + portName); |
91 |
$(this).unbind (); |
92 |
$(this).click (togglePortfast); |
93 |
}); |
94 |
$("a.errors").click (function () { |
95 |
$(this).attr ("class", "errorsload"); |
96 |
$(this).text ("Wait..."); |
97 |
td = $(this).parents ("td"); |
98 |
portName = $(this).parents("tr").attr ('id'); |
99 |
td.load ("update.py?action=geterrors&switch=%(switchName)s&port=" + portName); |
100 |
}); |
101 |
$("a.status").click (function () { |
102 |
$(this).attr ("class", "statusload"); |
103 |
$(this).text ("Wait..."); |
104 |
td = $(this).parents ("td"); |
105 |
portName = $(this).parents("tr").attr ('id'); |
106 |
$(this).load ("update.py?action=toggleshutdown&switch=%(switchName)s&port=" + portName); |
107 |
}); |
108 |
togglePortfast = function () { |
109 |
$(this).text ("Wait..."); |
110 |
portName = $(this).parents("tr").attr ('id'); |
111 |
$(this).load ("update.py?action=toggleportfast&switch=%(switchName)s&port=" + portName); |
112 |
}; |
113 |
toggleShutdown = function () { |
114 |
$(this).text ("Wait..."); |
115 |
portName = $(this).parents("tr").attr ('id'); |
116 |
$(this).load ("update.py?action=toggleshutdown&switch=%(switchName)s&port=" + portName); |
117 |
}; |
118 |
descClick = function () { |
119 |
$(this).attr ("class", "descinput"); |
120 |
portName = $(this).parents("tr").attr ('id'); |
121 |
td = $(this).parents ("td"); |
122 |
td.html ("<input type='text' value='" + $(this).text() + "' />"); |
123 |
td.children ().focus ().blur (function () { |
124 |
portName = $(this).parents("tr").attr ('id'); |
125 |
portDesc = $(this).attr ("value"); |
126 |
td = $(this).parents ("td"); |
127 |
td.html ("<a style='cursor:pointer'>Saving...</a>"); |
128 |
td.children ().click (descClick); |
129 |
td.children().load ("update.py?action=setdescription&switch=%(switchName)s&port=" + portName + "&desc=" + escape (portDesc)); |
130 |
}); |
131 |
//td.load ("update.py?action=geterrors&switch=%(switchName)s&port=" + portName); |
132 |
}; |
133 |
vlanClick = function () { |
134 |
$(this).attr ("class", "vlaninput"); |
135 |
portName = $(this).parents("tr").attr ('id'); |
136 |
td = $(this).parents ("td"); |
137 |
td.html ("<input type='text' value='" + $(this).text() + "' />"); |
138 |
td.children ().focus ().blur (function () { |
139 |
portName = $(this).parents("tr").attr ('id'); |
140 |
vlan = $(this).attr ("value"); |
141 |
td = $(this).parents ("td"); |
142 |
td.html ("<a style='cursor:pointer'>Saving...</a>"); |
143 |
td.children ().click (vlanClick); |
144 |
td.children().load ("update.py?action=setvlan&switch=%(switchName)s&port=" + escape(portName) + "&vlan=" + escape (vlan)); |
145 |
}); |
146 |
}; |
147 |
$("a.desc").click (descClick); |
148 |
$("a.vlan").click (vlanClick); |
149 |
}); |
150 |
</script> |
151 |
""" % {'switchName' : switchName} |
152 |
|
153 |
try: |
154 |
switch = pyaaa.SafeObject (pycisco.NetworkDevice (), switchName) |
155 |
switch._Resume (sessionID) |
156 |
switch._Remap ("Connect", {"hostname" : "hostname", "username" : "usern", "password" : "passw", "enable" : "enablepw"}) |
157 |
switch.Connect () |
158 |
ports = switch.GetPortSummary () |
159 |
except pexpect.TIMEOUT: |
160 |
pageText += "<h3 style='color:rgb(255,0,0)'>Switch appears to be down.</h3>" |
161 |
return pageText |
162 |
|
163 |
|
164 |
pageText += """ |
165 |
<table border='0' cellpadding='2' cellspacing='0' class='rowset' > |
166 |
<tr> |
167 |
<th>Number</th> |
168 |
<th>Description</th> |
169 |
<th>Connected</th> |
170 |
<th>VLAN</th> |
171 |
<th>Duplex</th> |
172 |
<th>Speed</th> |
173 |
<th>Media</th> |
174 |
<th>Portfast</th> |
175 |
<th>Errors</th> |
176 |
</tr> |
177 |
""" |
178 |
even = True |
179 |
for port in ports: |
180 |
pageText += """ |
181 |
<tr class='%s' id='%s'> |
182 |
<td>%s</td> |
183 |
<td><a style='cursor:pointer' class='desc'>%s</a></td> |
184 |
<td><a style='cursor:pointer' class='status'>%s</a></td> |
185 |
<td><a style='cursor:pointer' class='vlan'>%s</a></td> |
186 |
<td>%s</td><td>%s</td><td>%s</td> |
187 |
<td><a style='cursor:pointer' class='portfast'>%s</a></td> |
188 |
<td><a style='cursor:pointer' class='errors'>%s</td> |
189 |
</tr> |
190 |
""" % (even and "even" or "odd", port["name"], port["name"], port["description"] or "(not set)", port["connectstate"], port["vlan"], port["duplex"], port["speed"], port["media"], port["portfast"], port["errors"]) |
191 |
even = not even |
192 |
pageText += "</table>" |
193 |
|
194 |
return pageText |
195 |
|
196 |
|
197 |
def GetBody (): |
198 |
if not form.has_key ("action"): |
199 |
pageText = ShowDefaultPage () |
200 |
elif form["action"].value == "status": |
201 |
if not form.has_key ("switch"): |
202 |
pageText = "Missing switch argument, please stop trying to craft your own URLs!" |
203 |
else: |
204 |
pageText = ShowSwitchStatus (form["switch"].value) |
205 |
|
206 |
return pageText |
207 |
|
208 |
def GetSidebar (): |
209 |
pageText = """ |
210 |
<dt><a href="./">Navigation</a></dt> |
211 |
<dd><ul> |
212 |
<li><a href="./">Main Menu</a></li> |
213 |
""" |
214 |
if defaultCursor._admin == True: pageText += "<li><a href='./configure.py'>Configuration</a></li>" |
215 |
pageText += """ |
216 |
<li><a href="logout.py">Log Out</a></li> |
217 |
</ul></dd> |
218 |
<dt><a href="./">Switches</a></dt> |
219 |
<dd><ul> |
220 |
""" |
221 |
for switch in GetSwitchList (): |
222 |
pageText += '<li><a href="./?action=status&switch=' + switch.split(".")[0] + '">' + switch.split(".")[0] + '</a></li>' |
223 |
pageText += '</ul></dd>' |
224 |
return pageText |
225 |
|
226 |
switchList = [] |
227 |
pageBuffer = "" |
228 |
pageBuffer += "Content-Type: text/html\n\n" |
229 |
try: |
230 |
ProcessCookie () |
231 |
pageBuffer += common.GetHeader ("Port Administration Configuration Tool", GetSidebar ()) |
232 |
pageBuffer += GetBody () |
233 |
except pyaaa.DatabaseError: |
234 |
pageBuffer += common.GetHeader ("Error - Port Administration Configuration Tool", common.GetErrorSidebar ()) |
235 |
pageBuffer += "There was a problem with the database. Please contact your system administrator." |
236 |
except pyaaa.SessionExpired: |
237 |
print "Location: login.py" |
238 |
print |
239 |
pageBuffer += common.GetFooter () |
240 |
|
241 |
print pageBuffer |