/[shmookey]/portconf/index.py


UCC Code Repository

Contents of /portconf/index.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 117 - (hide annotations) (download) (as text)
Tue Jun 3 07:49:09 2008 UTC (12 years, 7 months ago) by shmookey
File MIME type: text/x-python
File size: 7386 byte(s)
login stuff all working, still needs authentication source

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

Properties

Name Value
svn:executable *

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26