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