/[shmookey]/portconf/index.py


UCC Code Repository

Contents of /portconf/index.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 120 - (hide annotations) (download) (as text)
Thu Jun 5 07:30:56 2008 UTC (12 years, 7 months ago) by shmookey
File MIME type: text/x-python
File size: 7782 byte(s)
general improvements, beginning stump of a configuration interface

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

Properties

Name Value
svn:executable *

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26