/[shmookey]/portconf/index.py


UCC Code Repository

Contents of /portconf/index.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 114 - (show annotations) (download) (as text)
Wed May 28 10:30:17 2008 UTC (12 years, 7 months ago) by shmookey
File MIME type: text/x-python
File size: 6954 byte(s)
cookies

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

Properties

Name Value
svn:executable *

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26