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