1 |
shmookey |
53 |
#!/usr/bin/python |
2 |
|
|
|
3 |
shmookey |
62 |
import sys, re, pexpect |
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 |
|
|
|
26 |
|
|
form = cgi.FieldStorage () |
27 |
|
|
|
28 |
shmookey |
110 |
defaultCursor = None |
29 |
|
|
def GetDefaultCursor (): |
30 |
|
|
global defaultCursor |
31 |
|
|
if defaultCursor == None: |
32 |
|
|
defaultCursor = pyaaa.SafeBase () |
33 |
|
|
defaultCursor._username = "me" |
34 |
|
|
return defaultCursor |
35 |
shmookey |
53 |
|
36 |
shmookey |
110 |
def GetSwitchList (): |
37 |
|
|
global switchList |
38 |
|
|
if switchList == []: |
39 |
|
|
switchList = GetDefaultCursor ()._GetRecords ("NetworkDevice") |
40 |
|
|
return switchList |
41 |
|
|
|
42 |
shmookey |
53 |
def ShowDefaultPage (): |
43 |
shmookey |
110 |
pageText = """ |
44 |
|
|
<p>Select a switch to get started!</p> |
45 |
|
|
<form action='index.py' method='get'> |
46 |
|
|
<select name='switch'> |
47 |
|
|
""" |
48 |
shmookey |
53 |
|
49 |
shmookey |
110 |
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 |
shmookey |
53 |
def ShowSwitchStatus (switchName): |
62 |
shmookey |
110 |
pageText = """ |
63 |
|
|
<h3>Port summary for %(switchName)s</h3> |
64 |
|
|
<script type='text/javascript'> |
65 |
shmookey |
53 |
$(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 |
shmookey |
110 |
$(this).load ("update.py?action=getportfast&switch=%(switchName)s&port=" + portName)s; |
73 |
shmookey |
53 |
$(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 |
shmookey |
110 |
td.load ("update.py?action=geterrors&switch=%(switchName)s&port=" + portName)s; |
82 |
shmookey |
53 |
}); |
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 |
shmookey |
110 |
$(this).load ("update.py?action=toggleshutdown&switch=%(switchName)s&port=" + portName)s; |
89 |
shmookey |
53 |
}); |
90 |
|
|
togglePortfast = function () { |
91 |
|
|
$(this).text ("Wait..."); |
92 |
|
|
portName = $(this).parents("tr").attr ('id'); |
93 |
shmookey |
110 |
$(this).load ("update.py?action=toggleportfast&switch=%(switchName)s&port=" + portName)s; |
94 |
shmookey |
53 |
}; |
95 |
|
|
toggleShutdown = function () { |
96 |
|
|
$(this).text ("Wait..."); |
97 |
|
|
portName = $(this).parents("tr").attr ('id'); |
98 |
shmookey |
110 |
$(this).load ("update.py?action=toggleshutdown&switch=%(switchName)s&port=" + portName)s; |
99 |
shmookey |
53 |
}; |
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 |
shmookey |
110 |
td.children().load ("update.py?action=setdescription&switch=%(switchName)s&port=" + portName + "&desc=" + escape (portDesc)); |
112 |
shmookey |
53 |
}); |
113 |
shmookey |
110 |
//td.load ("update.py?action=geterrors&switch=%(switchName)s&port=" + portName)s; |
114 |
shmookey |
53 |
}; |
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 |
shmookey |
110 |
td.children().load ("update.py?action=setvlan&switch=%(switchName)s&port=" + escape(portName)s + "&vlan=" + escape (vlan)); |
127 |
shmookey |
53 |
}); |
128 |
|
|
}; |
129 |
|
|
$("a.desc").click (descClick); |
130 |
|
|
$("a.vlan").click (vlanClick); |
131 |
|
|
}); |
132 |
shmookey |
110 |
</script> |
133 |
|
|
""" % {'switchName' : switchName} |
134 |
shmookey |
53 |
|
135 |
|
|
try: |
136 |
shmookey |
109 |
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 |
shmookey |
53 |
except pexpect.TIMEOUT: |
142 |
shmookey |
110 |
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 |
shmookey |
53 |
even = True |
161 |
|
|
for port in ports: |
162 |
shmookey |
110 |
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 |
shmookey |
53 |
even = not even |
174 |
shmookey |
110 |
pageText += "</table>" |
175 |
shmookey |
53 |
|
176 |
shmookey |
110 |
return pageText |
177 |
shmookey |
53 |
|
178 |
|
|
|
179 |
shmookey |
110 |
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 |