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