/[shmookey]/portconf/update.py


UCC Code Repository

Contents of /portconf/update.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 119 - (show annotations) (download) (as text)
Wed Jun 4 14:15:39 2008 UTC (12 years, 5 months ago) by shmookey
File MIME type: text/x-python
File size: 3723 byte(s)
authentication all working

1 #!/usr/bin/python
2 ''' update.py - pact main interface for ajax
3 '''
4
5 import sys, re, os, pexpect, Cookie
6 import Log
7
8 print "Content-Type: text/html"
9 print
10
11 try:
12 from Config import libPaths, dbName, dbUser, dbPass, dbHost, dbPort
13 except:
14 Log.Message ("Error occured in finding basic configuration paramaters. AJAX request failed.", "update.py", "ERROR")
15 print "Configuration failure."
16 sys.exit ()
17
18 for path in libPaths: sys.path.append (path)
19
20 try:
21 from pyaaa import SafeObject, SafeBase
22 import pycisco
23 except:
24 Log.Message ("Couldn't import pycisco or pyaaa. AJAX request failed.", "update.py", "ERROR")
25 print "Import failure."
26 sys.exit ()
27
28 import cgi, cgitb
29 cgitb.enable ()
30
31 # Cookie stuff
32 cookie = Cookie.SimpleCookie (os.environ.get("HTTP_COOKIE", ""))
33 try:
34 sessionID = cookie["sid"].value
35 obj = SafeBase ()
36 obj._Resume (sessionID)
37 except:
38 # Not logged in, fail.
39 print "Not logged in."
40 sys.exit ()
41
42
43 def RunPage (): pass
44
45 def LCR (fn):
46 def LoginConnectRun (name, *args):
47 obj = SafeObject (pycisco.NetworkDevice (), name)
48 obj._Resume (sessionID)
49 obj._Remap ("Connect", {"hostname" : "hostname", "username" : "usern", "password" : "passw", "enable" : "enablepw"})
50 obj.Connect ()
51 result = obj.__dict__[fn] (*args)
52 return result
53 return LoginConnectRun
54
55
56 ''' Connector functions! If you want your web front-end to be able to call Python functions, add them here.
57 COMMAND NAME: The "action" value in the GET request that you want to map a Python function to.
58 PYTHON FUNCTION: The function you want it to call.
59 ARGUMENT LIST: An ordered list of arguments to look for in the GET request string, and pass to the Python function.
60 RETURN VALUE TRANSLATION: A dictionary of mappings to transform the return value of the Python function into something
61 readable. If a "__default" key exists, use that if a more appropriate key isn't found. Otherwise,
62 just return the raw value. You can also use this field to do things after the function returns.
63 '''
64 actions = {
65 # COMMAND NAME PYTHON FUNCTION ARGUMENT LIST RETURN VALUE TRANSLATOR
66 "getportfast" : (LCR("CheckForPortfast"), ("switch", "port"), lambda x: X(x,{True : "On", False : "Off"})),
67 "geterrors" : (LCR("GetErrors"), ("switch", "port"), lambda x: X(x,{})),
68 "setdescription": (LCR("SetDescription"), ("switch", "port", "desc"), lambda x: X(x,{})),
69 "setvlan" : (LCR("SetVLAN"), ("switch", "port", "vlan"), lambda x: X(x,{})),
70 "toggleshutdown": (LCR("ToggleShutdown"), ("switch", "port"), lambda x: X(x,{})),
71 "toggleportfast": (LCR("TogglePortfast"), ("switch", "port"), lambda x: X(x,{})),
72 "login" : (LCR("__Login"), ("username", "password"), lambda x: X(x,{}))
73 }
74
75
76 # Exception handling down the bottom. You probably don't need to change these next two functions.
77 #
78
79 def LoginHandler (result):
80 if result == False:
81 print "Failed."
82
83
84 def X (key, translations):
85 if translations.has_key (key):
86 return translations [key]
87 if translations.has_key ("__default"):
88 return translations ["__default"]
89 return key
90
91 def RunPage ():
92 arglist = []
93 form = cgi.FieldStorage ()
94
95 if not form.has_key ("action"):
96 print "Error 1000"
97 return
98 action = form["action"].value
99 if not actions.has_key (action):
100 print "Error 1001"
101 return
102 for arg in actions[action][1]:
103 if not form.has_key (arg):
104 print "Error 1002"
105 return
106 arglist.append (form[arg].value)
107 result = actions[action][0] (*arglist)
108 print actions[action][2] (result)
109
110 try:
111 RunPage ()
112 except pycisco.NoSuchSwitchException:
113 print "Invalid"
114 except pycisco.TrunkException:
115 print "Disallowed"
116 except pycisco.VLANDisallowedException:
117 print "Disallowed"
118 #except EOF:
119 # print "Too many sessions!"

Properties

Name Value
svn:executable *

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26