1 |
#!/usr/bin/python |
2 |
|
3 |
import sys, re, pexpect |
4 |
|
5 |
try: |
6 |
from Config import libPaths, dbName, dbUser, dbPass, dbAddress, dbPort |
7 |
except ImportError: |
8 |
import install |
9 |
sys.exit () |
10 |
|
11 |
import cgi, cgitb |
12 |
cgitb.enable () |
13 |
print "Content-Type: text/html" |
14 |
print |
15 |
|
16 |
import pycisco |
17 |
|
18 |
print "<h2>Port Administration Configuration Tool</h2>" |
19 |
|
20 |
form = cgi.FieldStorage () |
21 |
|
22 |
|
23 |
def ShowDefaultPage (): |
24 |
print "<p>Select a switch to get started!</p>" |
25 |
print "<form action='index.html' method='get'>" |
26 |
print "<select name='switch'>" |
27 |
switches = pycisco.GetSwitches () |
28 |
for switch in switches: |
29 |
print "<option value='" + switch + "'>" + switch + "</option>" |
30 |
print "</select>" |
31 |
print "<input type='hidden' name='action' value='status' />" |
32 |
print "<input type='submit' value='View Port Summary' />" |
33 |
print "</form>" |
34 |
|
35 |
def ShowSwitchStatus (switchName): |
36 |
print "<h3>Port summary for " + switchName + "</h3>" |
37 |
|
38 |
print "<script type='text/javascript'>" |
39 |
print """ |
40 |
$(document).ready (function () { |
41 |
$("a.portfast").click (function () { |
42 |
// Change the appearance and class so that it's no longer a "reveal"-style link |
43 |
$(this).attr ("class", "portfastload"); |
44 |
$(this).text ("Wait..."); |
45 |
|
46 |
portName = $(this).parents("tr").attr ('id'); |
47 |
$(this).load ("update.py?action=getportfast&switch=""" + switchName + """&port=" + portName); |
48 |
$(this).unbind (); |
49 |
$(this).click (togglePortfast); |
50 |
}); |
51 |
$("a.errors").click (function () { |
52 |
$(this).attr ("class", "errorsload"); |
53 |
$(this).text ("Wait..."); |
54 |
td = $(this).parents ("td"); |
55 |
portName = $(this).parents("tr").attr ('id'); |
56 |
td.load ("update.py?action=geterrors&switch=""" + switchName + """&port=" + portName); |
57 |
}); |
58 |
$("a.status").click (function () { |
59 |
$(this).attr ("class", "statusload"); |
60 |
$(this).text ("Wait..."); |
61 |
td = $(this).parents ("td"); |
62 |
portName = $(this).parents("tr").attr ('id'); |
63 |
$(this).load ("update.py?action=toggleshutdown&switch=""" + switchName + """&port=" + portName); |
64 |
}); |
65 |
togglePortfast = function () { |
66 |
$(this).text ("Wait..."); |
67 |
portName = $(this).parents("tr").attr ('id'); |
68 |
$(this).load ("update.py?action=toggleportfast&switch=""" + switchName + """&port=" + portName); |
69 |
}; |
70 |
toggleShutdown = function () { |
71 |
$(this).text ("Wait..."); |
72 |
portName = $(this).parents("tr").attr ('id'); |
73 |
$(this).load ("update.py?action=toggleshutdown&switch=""" + switchName + """&port=" + portName); |
74 |
}; |
75 |
descClick = function () { |
76 |
$(this).attr ("class", "descinput"); |
77 |
portName = $(this).parents("tr").attr ('id'); |
78 |
td = $(this).parents ("td"); |
79 |
td.html ("<input type='text' value='" + $(this).text() + "' />"); |
80 |
td.children ().focus ().blur (function () { |
81 |
portName = $(this).parents("tr").attr ('id'); |
82 |
portDesc = $(this).attr ("value"); |
83 |
td = $(this).parents ("td"); |
84 |
td.html ("<a style='cursor:pointer'>Saving...</a>"); |
85 |
td.children ().click (descClick); |
86 |
td.children().load ("update.py?action=setdescription&switch=""" + switchName + """&port=" + portName + "&desc=" + escape (portDesc)); |
87 |
}); |
88 |
//td.load ("update.py?action=geterrors&switch=""" + switchName + """&port=" + portName); |
89 |
}; |
90 |
vlanClick = function () { |
91 |
$(this).attr ("class", "vlaninput"); |
92 |
portName = $(this).parents("tr").attr ('id'); |
93 |
td = $(this).parents ("td"); |
94 |
td.html ("<input type='text' value='" + $(this).text() + "' />"); |
95 |
td.children ().focus ().blur (function () { |
96 |
portName = $(this).parents("tr").attr ('id'); |
97 |
vlan = $(this).attr ("value"); |
98 |
td = $(this).parents ("td"); |
99 |
td.html ("<a style='cursor:pointer'>Saving...</a>"); |
100 |
td.children ().click (vlanClick); |
101 |
td.children().load ("update.py?action=setvlan&switch=""" + switchName + """&port=" + escape(portName) + "&vlan=" + escape (vlan)); |
102 |
}); |
103 |
}; |
104 |
$("a.desc").click (descClick); |
105 |
$("a.vlan").click (vlanClick); |
106 |
}); |
107 |
""" |
108 |
print "</script>" |
109 |
|
110 |
try: |
111 |
ports = pycisco.GetPortSummary (switchName) |
112 |
except pexpect.TIMEOUT: |
113 |
print "<h3 style='color:rgb(255,0,0)'>Switch appears to be down.</h3>" |
114 |
return |
115 |
print "<table border='0' cellpadding='2' cellspacing='0' class='rowset' >" |
116 |
print "<tr>" |
117 |
print "<th>Number</th>" |
118 |
print "<th>Description</th>" |
119 |
print "<th>Connected</th>" |
120 |
print "<th>VLAN</th>" |
121 |
print "<th>Duplex</th>" |
122 |
print "<th>Speed</th>" |
123 |
print "<th>Media</th>" |
124 |
print "<th>Portfast</th>" |
125 |
print "<th>Errors</th>" |
126 |
print "</tr>" |
127 |
even = True |
128 |
for port in ports: |
129 |
print "<tr class='" + (even and "even" or "odd") + "' id='" + port["name"] + "'>" |
130 |
print "<td>" + port["name"] + "</td>" |
131 |
print "<td><a style='cursor:pointer' class='desc'>" + (port["description"] or "(not set)") + "</a></td>" |
132 |
print "<td><a style='cursor:pointer' class='status'>" + port["connectstate"] + "</a></td>" |
133 |
print "<td><a style='cursor:pointer' class='vlan'>" + port["vlan"] + "</a></td>" |
134 |
print "<td>" + port["duplex"] + "</td>" |
135 |
print "<td>" + port["speed"] + "</td>" |
136 |
print "<td>" + port["media"] + "</td>" |
137 |
print "<td><a style='cursor:pointer' class='portfast'>" + port["portfast"] + "</a></td>" |
138 |
print "<td><a style='cursor:pointer' class='errors'>" + port["errors"] + "</td>" |
139 |
print "</tr>" |
140 |
even = not even |
141 |
print "</table>" |
142 |
|
143 |
|
144 |
if not form.has_key ("action"): |
145 |
ShowDefaultPage () |
146 |
elif form["action"].value == "status": |
147 |
if not form.has_key ("switch"): |
148 |
print "Missing switch argument, please stop trying to craft your own URLs!" |
149 |
else: |
150 |
ShowSwitchStatus (form["switch"].value) |
151 |
|
152 |
|