#!/usr/bin/python import sys, os, re, pexpect, time, Cookie import Log, common try: from Config import libPaths, dbName, dbUser, dbPass, dbHost, dbPort except: Log.Message ("Error occured in finding basic configuration paramaters. Reverting to installer.", "index.py", "ERROR") import install sys.exit () for path in libPaths: sys.path.append (path) try: import pycisco, pyaaa except: Log.Message ("Couldn't import pycisco or pyaaa. Reverting to installer.", "index.py", "ERROR") import install sys.exit () import cgi, cgitb cgitb.enable () form = cgi.FieldStorage () defaultCursor = None def GetDefaultCursor (): global defaultCursor if defaultCursor == None: defaultCursor = pyaaa.SafeBase () defaultCursor._Resume (sessionID) return defaultCursor # Cookie stuff sessionID = None def ProcessCookie (): global sessionID cookie = Cookie.SimpleCookie (os.environ.get("HTTP_COOKIE", "")) if cookie.has_key ("sid"): sessionID = cookie["sid"].value try: GetDefaultCursor () except pyaaa.InvalidSession: # TODO: Remember when this happens to catch people trying to brute-force session IDs. Log.Message ("Invalid session ID: " + sessionID + ". Redirecting to login.", "index.py", "ERROR") print "Location: login.py" else: # Not logged in, fail. Log.Message ("User not logged in. Redirecting to login.", "index.py", "ERROR") print "Location: login.py" def GetSwitchList (): global switchList if switchList == []: switchList = GetDefaultCursor ()._GetRecords ("NetworkDevice") return switchList def ShowDefaultPage (): pageText = """

Select a switch to get started!

""" return pageText def ShowSwitchStatus (switchName): pageText = """

Port summary for %(switchName)s

""" % {'switchName' : switchName} try: switch = pyaaa.SafeObject (pycisco.NetworkDevice (), switchName) switch._Resume (sessionID) switch._Remap ("Connect", {"hostname" : "hostname", "username" : "usern", "password" : "passw", "enable" : "enablepw"}) switch.Connect () ports = switch.GetPortSummary () except pexpect.TIMEOUT: pageText += "

Switch appears to be down.

" return pageText pageText += """ """ even = True for port in ports: pageText += """ """ % (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"]) even = not even pageText += "
Number Description Connected VLAN Duplex Speed Media Portfast Errors
%s %s %s %s %s%s%s %s %s
" return pageText def GetBody (): if not form.has_key ("action"): pageText = ShowDefaultPage () elif form["action"].value == "status": if not form.has_key ("switch"): pageText = "Missing switch argument, please stop trying to craft your own URLs!" else: pageText = ShowSwitchStatus (form["switch"].value) return pageText def GetSidebar (): pageText = """
Navigation
Switches
' return pageText switchList = [] pageBuffer = "" pageBuffer += "Content-Type: text/html\n\n" try: ProcessCookie () pageBuffer += common.GetHeader ("Port Administration Configuration Tool", GetSidebar ()) pageBuffer += GetBody () except pyaaa.DatabaseError: pageBuffer += common.GetHeader ("Error - Port Administration Configuration Tool", common.GetErrorSidebar ()) pageBuffer += "There was a problem with the database. Please contact your system administrator." except pyaaa.SessionExpired: print "Location: login.py" print pageBuffer += common.GetFooter () print pageBuffer