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