[shmookey] / portconf / update.py Repository:


UCC Code Repository

View of /portconf/update.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 119 - (download) (as text) (annotate)
Wed Jun 4 14:15:39 2008 UTC (2 years, 3 months ago) by shmookey
File size: 3723 byte(s)
authentication all working

#!/usr/bin/python
''' update.py - pact main interface for ajax
'''

import sys, re, os, pexpect, Cookie
import Log

print "Content-Type: text/html"
print

try:
        from Config import libPaths, dbName, dbUser, dbPass, dbHost, dbPort
except: 
        Log.Message ("Error occured in finding basic configuration paramaters. AJAX request failed.", "update.py", "ERROR")
        print "Configuration failure."
        sys.exit ()

for path in libPaths: sys.path.append (path)

try:
	from pyaaa import SafeObject, SafeBase
        import pycisco
except:
        Log.Message ("Couldn't import pycisco or pyaaa. AJAX request failed.", "update.py", "ERROR")
	print "Import failure."
	sys.exit ()

import cgi, cgitb
cgitb.enable ()

# Cookie stuff
cookie = Cookie.SimpleCookie (os.environ.get("HTTP_COOKIE", ""))
try:
	sessionID = cookie["sid"].value
	obj = SafeBase ()
	obj._Resume (sessionID)
except:
	# Not logged in, fail. 
	print "Not logged in."
	sys.exit ()


def RunPage (): pass

def LCR (fn):
	def LoginConnectRun (name, *args):
		obj = SafeObject (pycisco.NetworkDevice (), name)
		obj._Resume (sessionID)
		obj._Remap ("Connect", {"hostname" : "hostname", "username" : "usern", "password" : "passw", "enable" : "enablepw"})
		obj.Connect ()
		result = obj.__dict__[fn] (*args)
		return result
	return LoginConnectRun


''' Connector functions! If you want your web front-end to be able to call Python functions, add them here. 
 COMMAND NAME: 			The "action" value in the GET request that you want to map a Python function to.
 PYTHON FUNCTION: 		The function you want it to call.
 ARGUMENT LIST:			An ordered list of arguments to look for in the GET request string, and pass to the Python function.
 RETURN VALUE TRANSLATION: 	A dictionary of mappings to transform the return value of the Python function into something
 				readable. If a "__default" key exists, use that if a more appropriate key isn't found. Otherwise,
				just return the raw value. You can also use this field to do things after the function returns.
'''
actions = {	
#	COMMAND NAME	   PYTHON FUNCTION		ARGUMENT LIST			RETURN VALUE TRANSLATOR
	"getportfast"	: (LCR("CheckForPortfast"), 	("switch", "port"),		lambda x: X(x,{True : "On", False : "Off"})),
	"geterrors"	: (LCR("GetErrors"), 		("switch", "port"),		lambda x: X(x,{})),
	"setdescription": (LCR("SetDescription"), 	("switch", "port", "desc"),	lambda x: X(x,{})),
	"setvlan"	: (LCR("SetVLAN"),		("switch", "port", "vlan"),	lambda x: X(x,{})),
	"toggleshutdown": (LCR("ToggleShutdown"), 	("switch", "port"),		lambda x: X(x,{})),
	"toggleportfast": (LCR("TogglePortfast"), 	("switch", "port"),		lambda x: X(x,{})),
	"login"		: (LCR("__Login"),		("username", "password"),	lambda x: X(x,{}))
}


# Exception handling down the bottom. You probably don't need to change these next two functions.
#

def LoginHandler (result):
	if result == False:
		print "Failed."
	

def X (key, translations):
	if translations.has_key (key):
		return translations [key]
	if translations.has_key ("__default"):
		return translations ["__default"]
	return key

def RunPage ():
	arglist = []
	form = cgi.FieldStorage ()
	
	if not form.has_key ("action"):
		print "Error 1000"
		return
	action = form["action"].value
	if not actions.has_key (action):
		print "Error 1001"
		return
	for arg in actions[action][1]:
		if not form.has_key (arg):
			print "Error 1002"
			return
		arglist.append (form[arg].value)
	result = actions[action][0] (*arglist)
	print actions[action][2] (result)
	
try:
	RunPage ()
except pycisco.NoSuchSwitchException:
	print "Invalid"
except pycisco.TrunkException:
	print "Disallowed"
except pycisco.VLANDisallowedException:
	print "Disallowed"
#except EOF:
#	print "Too many sessions!"

UCC Webmasters
ViewVC Help
Powered by ViewVC 1.0.5