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