/[shmookey]/portconf/index.py


UCC Code Repository

Contents of /portconf/index.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 113 - (show annotations) (download) (as text)
Wed May 28 09:44:41 2008 UTC (12 years, 7 months ago) by shmookey
File MIME type: text/x-python
File size: 6815 byte(s)
start on real authentication

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 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 defaultCursor = None
33 def GetDefaultCursor ():
34 global defaultCursor
35 if defaultCursor == None:
36 defaultCursor = pyaaa.SafeBase ()
37 defaultCursor._username = "me"
38 return defaultCursor
39
40 def GetSwitchList ():
41 global switchList
42 if switchList == []:
43 switchList = GetDefaultCursor ()._GetRecords ("NetworkDevice")
44 return switchList
45
46 def ShowDefaultPage ():
47 pageText = """
48 <p>Select a switch to get started!</p>
49 <form action='index.py' method='get'>
50 <select name='switch'>
51 """
52
53 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 def ShowSwitchStatus (switchName):
66 pageText = """
67 <h3>Port summary for %(switchName)s</h3>
68 <script type='text/javascript'>
69 $(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 $(this).load ("update.py?action=getportfast&switch=%(switchName)s&port=" + portName);
77 $(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 td.load ("update.py?action=geterrors&switch=%(switchName)s&port=" + portName);
86 });
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 $(this).load ("update.py?action=toggleshutdown&switch=%(switchName)s&port=" + portName);
93 });
94 togglePortfast = function () {
95 $(this).text ("Wait...");
96 portName = $(this).parents("tr").attr ('id');
97 $(this).load ("update.py?action=toggleportfast&switch=%(switchName)s&port=" + portName);
98 };
99 toggleShutdown = function () {
100 $(this).text ("Wait...");
101 portName = $(this).parents("tr").attr ('id');
102 $(this).load ("update.py?action=toggleshutdown&switch=%(switchName)s&port=" + portName);
103 };
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 td.children().load ("update.py?action=setdescription&switch=%(switchName)s&port=" + portName + "&desc=" + escape (portDesc));
116 });
117 //td.load ("update.py?action=geterrors&switch=%(switchName)s&port=" + portName);
118 };
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 td.children().load ("update.py?action=setvlan&switch=%(switchName)s&port=" + escape(portName) + "&vlan=" + escape (vlan));
131 });
132 };
133 $("a.desc").click (descClick);
134 $("a.vlan").click (vlanClick);
135 });
136 </script>
137 """ % {'switchName' : switchName}
138
139 try:
140 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 except pexpect.TIMEOUT:
146 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 even = True
165 for port in ports:
166 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 even = not even
178 pageText += "</table>"
179
180 return pageText
181
182
183 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

Properties

Name Value
svn:executable *

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26