1 |
#!/usr/bin/python |
#!/usr/bin/python |
2 |
|
|
3 |
import sys, re, pexpect |
import sys, re, pexpect |
4 |
import Log |
import Log, common |
5 |
|
|
6 |
try: |
try: |
7 |
from Config import libPaths, dbName, dbUser, dbPass, dbHost, dbPort |
from Config import libPaths, dbName, dbUser, dbPass, dbHost, dbPort |
21 |
|
|
22 |
import cgi, cgitb |
import cgi, cgitb |
23 |
cgitb.enable () |
cgitb.enable () |
|
print "Content-Type: text/html" |
|
|
print |
|
|
print """ |
|
|
<!--#set var="TITLE" value="Port Administration Configuration Tool" --> |
|
|
<!--#set var="SIDEBAR" value="../sidebar.py" --> |
|
|
<!--#include virtual="includes/header.html" --> |
|
|
""" |
|
24 |
|
|
|
print "<h2>Port Administration Configuration Tool</h2>" |
|
25 |
|
|
26 |
form = cgi.FieldStorage () |
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 (): |
def ShowDefaultPage (): |
43 |
print "<p>Select a switch to get started!</p>" |
pageText = """ |
44 |
print "<form action='index.py' method='get'>" |
<p>Select a switch to get started!</p> |
45 |
print "<select name='switch'>" |
<form action='index.py' method='get'> |
46 |
for switch in switchList: |
<select name='switch'> |
47 |
print "<option value='" + switch + "'>" + switch + "</option>" |
""" |
|
print "</select>" |
|
|
print "<input type='hidden' name='action' value='status' />" |
|
|
print "<input type='submit' value='View Port Summary' />" |
|
|
print "</form>" |
|
48 |
|
|
49 |
def ShowSwitchStatus (switchName): |
for switch in GetSwitchList (): |
50 |
print "<h3>Port summary for " + switchName + "</h3>" |
pageText += "<option value='" + switch.split(".")[0] + "'>" + switch.split(".")[0] + "</option>" |
51 |
|
|
52 |
print "<script type='text/javascript'>" |
pageText += """ |
53 |
print """ |
</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 () { |
$(document).ready (function () { |
66 |
$("a.portfast").click (function () { |
$("a.portfast").click (function () { |
67 |
// Change the appearance and class so that it's no longer a "reveal"-style link |
// Change the appearance and class so that it's no longer a "reveal"-style link |
69 |
$(this).text ("Wait..."); |
$(this).text ("Wait..."); |
70 |
|
|
71 |
portName = $(this).parents("tr").attr ('id'); |
portName = $(this).parents("tr").attr ('id'); |
72 |
$(this).load ("update.py?action=getportfast&switch=""" + switchName + """&port=" + portName); |
$(this).load ("update.py?action=getportfast&switch=%(switchName)s&port=" + portName)s; |
73 |
$(this).unbind (); |
$(this).unbind (); |
74 |
$(this).click (togglePortfast); |
$(this).click (togglePortfast); |
75 |
}); |
}); |
78 |
$(this).text ("Wait..."); |
$(this).text ("Wait..."); |
79 |
td = $(this).parents ("td"); |
td = $(this).parents ("td"); |
80 |
portName = $(this).parents("tr").attr ('id'); |
portName = $(this).parents("tr").attr ('id'); |
81 |
td.load ("update.py?action=geterrors&switch=""" + switchName + """&port=" + portName); |
td.load ("update.py?action=geterrors&switch=%(switchName)s&port=" + portName)s; |
82 |
}); |
}); |
83 |
$("a.status").click (function () { |
$("a.status").click (function () { |
84 |
$(this).attr ("class", "statusload"); |
$(this).attr ("class", "statusload"); |
85 |
$(this).text ("Wait..."); |
$(this).text ("Wait..."); |
86 |
td = $(this).parents ("td"); |
td = $(this).parents ("td"); |
87 |
portName = $(this).parents("tr").attr ('id'); |
portName = $(this).parents("tr").attr ('id'); |
88 |
$(this).load ("update.py?action=toggleshutdown&switch=""" + switchName + """&port=" + portName); |
$(this).load ("update.py?action=toggleshutdown&switch=%(switchName)s&port=" + portName)s; |
89 |
}); |
}); |
90 |
togglePortfast = function () { |
togglePortfast = function () { |
91 |
$(this).text ("Wait..."); |
$(this).text ("Wait..."); |
92 |
portName = $(this).parents("tr").attr ('id'); |
portName = $(this).parents("tr").attr ('id'); |
93 |
$(this).load ("update.py?action=toggleportfast&switch=""" + switchName + """&port=" + portName); |
$(this).load ("update.py?action=toggleportfast&switch=%(switchName)s&port=" + portName)s; |
94 |
}; |
}; |
95 |
toggleShutdown = function () { |
toggleShutdown = function () { |
96 |
$(this).text ("Wait..."); |
$(this).text ("Wait..."); |
97 |
portName = $(this).parents("tr").attr ('id'); |
portName = $(this).parents("tr").attr ('id'); |
98 |
$(this).load ("update.py?action=toggleshutdown&switch=""" + switchName + """&port=" + portName); |
$(this).load ("update.py?action=toggleshutdown&switch=%(switchName)s&port=" + portName)s; |
99 |
}; |
}; |
100 |
descClick = function () { |
descClick = function () { |
101 |
$(this).attr ("class", "descinput"); |
$(this).attr ("class", "descinput"); |
108 |
td = $(this).parents ("td"); |
td = $(this).parents ("td"); |
109 |
td.html ("<a style='cursor:pointer'>Saving...</a>"); |
td.html ("<a style='cursor:pointer'>Saving...</a>"); |
110 |
td.children ().click (descClick); |
td.children ().click (descClick); |
111 |
td.children().load ("update.py?action=setdescription&switch=""" + switchName + """&port=" + portName + "&desc=" + escape (portDesc)); |
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 + """&port=" + portName); |
//td.load ("update.py?action=geterrors&switch=%(switchName)s&port=" + portName)s; |
114 |
}; |
}; |
115 |
vlanClick = function () { |
vlanClick = function () { |
116 |
$(this).attr ("class", "vlaninput"); |
$(this).attr ("class", "vlaninput"); |
123 |
td = $(this).parents ("td"); |
td = $(this).parents ("td"); |
124 |
td.html ("<a style='cursor:pointer'>Saving...</a>"); |
td.html ("<a style='cursor:pointer'>Saving...</a>"); |
125 |
td.children ().click (vlanClick); |
td.children ().click (vlanClick); |
126 |
td.children().load ("update.py?action=setvlan&switch=""" + switchName + """&port=" + escape(portName) + "&vlan=" + escape (vlan)); |
td.children().load ("update.py?action=setvlan&switch=%(switchName)s&port=" + escape(portName)s + "&vlan=" + escape (vlan)); |
127 |
}); |
}); |
128 |
}; |
}; |
129 |
$("a.desc").click (descClick); |
$("a.desc").click (descClick); |
130 |
$("a.vlan").click (vlanClick); |
$("a.vlan").click (vlanClick); |
131 |
}); |
}); |
132 |
""" |
</script> |
133 |
print "</script>" |
""" % {'switchName' : switchName} |
134 |
|
|
135 |
try: |
try: |
136 |
switch = pyaaa.SafeObject (pycisco.NetworkDevice (), switchName) |
switch = pyaaa.SafeObject (pycisco.NetworkDevice (), switchName) |
139 |
switch.Connect () |
switch.Connect () |
140 |
ports = switch.GetPortSummary () |
ports = switch.GetPortSummary () |
141 |
except pexpect.TIMEOUT: |
except pexpect.TIMEOUT: |
142 |
print "<h3 style='color:rgb(255,0,0)'>Switch appears to be down.</h3>" |
pageText += "<h3 style='color:rgb(255,0,0)'>Switch appears to be down.</h3>" |
143 |
return |
return pageText |
144 |
print "<table border='0' cellpadding='2' cellspacing='0' class='rowset' >" |
|
145 |
print "<tr>" |
|
146 |
print "<th>Number</th>" |
pageText += """ |
147 |
print "<th>Description</th>" |
<table border='0' cellpadding='2' cellspacing='0' class='rowset' > |
148 |
print "<th>Connected</th>" |
<tr> |
149 |
print "<th>VLAN</th>" |
<th>Number</th> |
150 |
print "<th>Duplex</th>" |
<th>Description</th> |
151 |
print "<th>Speed</th>" |
<th>Connected</th> |
152 |
print "<th>Media</th>" |
<th>VLAN</th> |
153 |
print "<th>Portfast</th>" |
<th>Duplex</th> |
154 |
print "<th>Errors</th>" |
<th>Speed</th> |
155 |
print "</tr>" |
<th>Media</th> |
156 |
|
<th>Portfast</th> |
157 |
|
<th>Errors</th> |
158 |
|
</tr> |
159 |
|
""" |
160 |
even = True |
even = True |
161 |
for port in ports: |
for port in ports: |
162 |
print "<tr class='" + (even and "even" or "odd") + "' id='" + port["name"] + "'>" |
pageText += """ |
163 |
print "<td>" + port["name"] + "</td>" |
<tr class='%s' id='%s'> |
164 |
print "<td><a style='cursor:pointer' class='desc'>" + (port["description"] or "(not set)") + "</a></td>" |
<td>%s</td> |
165 |
print "<td><a style='cursor:pointer' class='status'>" + port["connectstate"] + "</a></td>" |
<td><a style='cursor:pointer' class='desc'>%s</a></td> |
166 |
print "<td><a style='cursor:pointer' class='vlan'>" + port["vlan"] + "</a></td>" |
<td><a style='cursor:pointer' class='status'>%s</a></td> |
167 |
print "<td>" + port["duplex"] + "</td>" |
<td><a style='cursor:pointer' class='vlan'>%s</a></td> |
168 |
print "<td>" + port["speed"] + "</td>" |
<td>%s</td><td>%s</td><td>%s</td> |
169 |
print "<td>" + port["media"] + "</td>" |
<td><a style='cursor:pointer' class='portfast'>%s</a></td> |
170 |
print "<td><a style='cursor:pointer' class='portfast'>" + port["portfast"] + "</a></td>" |
<td><a style='cursor:pointer' class='errors'>%s</td> |
171 |
print "<td><a style='cursor:pointer' class='errors'>" + port["errors"] + "</td>" |
</tr> |
172 |
print "</tr>" |
""" % (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 |
even = not even |
174 |
print "</table>" |
pageText += "</table>" |
175 |
|
|
176 |
|
return pageText |
177 |
|
|
178 |
|
|
179 |
if not form.has_key ("action"): |
def GetBody (): |
180 |
swInfo = pyaaa.SafeBase () |
if not form.has_key ("action"): |
181 |
swInfo._username = "me" |
pageText = ShowDefaultPage () |
182 |
switchList = swInfo._GetRecords ("NetworkDevice") |
elif form["action"].value == "status": |
183 |
ShowDefaultPage () |
if not form.has_key ("switch"): |
184 |
elif form["action"].value == "status": |
pageText = "Missing switch argument, please stop trying to craft your own URLs!" |
185 |
if not form.has_key ("switch"): |
else: |
186 |
print "Missing switch argument, please stop trying to craft your own URLs!" |
pageText = ShowSwitchStatus (form["switch"].value) |
187 |
else: |
|
188 |
ShowSwitchStatus (form["switch"].value) |
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 """<!--#include virtual="includes/footer.html" -->""" |
print pageBuffer |