[shmookey] / portconf / .pycisco.py.swp Repository:


UCC Code Repository

View of /portconf/.pycisco.py.swp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 53 - (download) (annotate)
Thu Mar 13 05:47:17 2008 UTC (2 years, 5 months ago) by shmookey
File size: 20480 byte(s)
Initial import.

b0VIM 6.37GDproottyphaon/var/www/portconf/pycisco.pyU3210#"! Utpxfy|A\ad%xrq.*)	}d#








h
b
a
?
9
8





|eb"



e
D
'

						v	t	.	^/
t7p6c=t\A@w[%#L4				words = line.split ()		portDetails = dict ()		# Create a new dictionary for the port description and split it up into words.	for line in lines:	# Parse the output of the port summary. Still easier than SNMP!		portSummary = []	lines = result.split ("\n")		result = "\n".join (raw_result.split ("\r\n")[3:-1])	# whitespace and headings.	# This next line chops off the first three lines, which are invariably		raw_result = ss.before	ss.expect ("[a-z0-9.-]*#")	ss.sendline ("show interface status")	ss = CreateSession (switchNumber)		return "No such switch!"	if switchNumber == -1:	switchNumber = GetSwitchNumber (switchName)def GetPortSummary (switchName):## errors - always "Check" in this (telnet) implementation.# portfast - always "Check" in this (telnet) implementation.# media - the media type of the port.# speed - the speed the port is running at if forced, otherwise "auto".# duplex - the duplex mode of the port if forced, otherwise "auto".# vlan - the VLAN number the port is configured to accces, or "Trunk".# connectstate - the current connected state of the port.# description - the description on the port, truncated at 19 characters.# name - the name of the port in shortened form.# The keys in the dictionary are always strings:## 'show interface status'# ports on the switch provided as an argument, gathered from# Returns a list of dictionaries containing information about the## Interface function.# GetPortSummary (switchName : string)#	return switchNumber			break			switchNumber = switch		if switchName == Switches.switches [switch]:	for switch in range (len (Switches.switches)):	switchNumber = -1def GetSwitchNumber (switchName):	## switch name for security reasons.# to satisfy functions like CreateSession () which do not accept a# Looks up the index of a given device name in the device table. Used## Utility function.# GetSwitchNumber (switchName : string)#	return session	session.expect ("[a-z0-9.-]*#")	session.sendline (Config.enable)	session.expect ("Password: ")	session.sendline ("enable")	session.expect ("[a-z0-9.-]*>")	session.sendline (Config.password)	session.expect ("Password: ")	session.sendline (Config.username)	session.expect ("Username: ")	session.setecho (False)	session.setwinsize (480,640)	session = spawn ("telnet " + Switches.switches [switchNumber])def CreateSession (switchNumber):## accidentally executed or passed around to places we don't want it to go.# of a switch name so that the string provided by the user will never be# Uses an index in the switches table rather than a string representation# the telnet session. # Logs on to a switch with telnet and returns a pexpect object containing## Helper function.# CreateSession (switchNumber : integer)#	passclass VLANDisallowedException (Exception):	passclass TrunkException (Exception):	passclass NoSuchSwitchException (Exception):## Exceptions#'''Utility functions - formatting and processing, may uses externallyHelper functions - used internally to collect raw dataInterface functions - useful to people using pycisco as a modulehere into three classes:''' For easier understanding, I have categorized the functionsimport Config, Switchesfrom string import ascii_letters, digits, maketransimport refrom pexpect import *'''Licensed under an MIT-style license: see LICENSE file for details.Requires Python 2.4 and pexpect.Written by Luke Williams <shmookey@shmookey.net>''' pycisco - a library for interacting with Cisco devices.ad(@AJ42gF#"



[
Z
6

c:D'%$



n
m
=

					c	6	
	c@?	ss.sendline ("exit\nexit\n\exit")	return GetConnectedStatus (switchName, portNumber, ss)	ss.expect ("[a-z0-9.-]*\(config-if\)#")	ss.sendline ( ((not currentValue) and [""] or ["no"])[0] + " shutdown")	ss.expect ("[a-z0-9.-]*\(config-if\)#")	ss.sendline ("interface " + safePortNumber)	ss.expect ("[a-z0-9.-]*\(config\)#")	ss.sendline ("configure terminal")	currentValue = CheckForShutdown (ss, safePortNumber)		return "Operation not allowed on trunk port!"	if CheckForTrunk (ss, safePortNumber):	safePortNumber = SanitisePortName (portNumber)	ss = CreateSession (switchNumber)		raise NoSuchSwitchException ()	if switchNumber == -1:	switchNumber = GetSwitchNumber (switchName)def ToggleShutdown (switchName, portNumber):## port after this operation.# Shuts down and enables the given port. Returns the "connected" status of the## Interface function.# ToggleShutdown (switchName : string, portNumber : string)#	return (not currentValue) and "On" or "Off"	ss.sendline ("exit\nexit\n\exit")	ss.expect ("[a-z0-9.-]*\(config-if\)#")	ss.sendline ( ((not currentValue) and [""] or ["no"])[0] + " spanning-tree portfast")	ss.expect ("[a-z0-9.-]*\(config-if\)#")	ss.sendline ("interface " + safePortNumber)	ss.expect ("[a-z0-9.-]*\(config\)#")	ss.sendline ("configure terminal")	currentValue = CheckForPortfast (switchName, portNumber, ss)		return "Operation not allowed on trunk port!"	if CheckForTrunk (ss, safePortNumber):	safePortNumber = SanitisePortName (portNumber)	ss = CreateSession (switchNumber)		raise NoSuchSwitchException ()	if switchNumber == -1:	switchNumber = GetSwitchNumber (switchName)def TogglePortfast (switchName, portNumber):## "On" or "Off".# Toggles the portfast option on and off for the given port. Returns## Interface function.# TogglePortfast (switchName : string, portNumber : string)#	return Switches.switchesdef GetSwitches ():## Returns a list of current allowable switches.## Utility function.ad
|`=<e?



v
t
r
.


gO.
vSR.


Y
W
J
H
F
			i	:	8	7	nJC.-x\1#-nL(!Q5!{g<$
k[-"!
## GetSwitches ()#			yield c		if c in (ascii_letters + digits + " \/()"):	for c in desc:def SanitizeDescription (desc):## Generator to remove unwanted characters from a port description.## Utility function.# SanitizeDescription (desc : string)#		return "Mystery!"		return "err-disabled"	if not result.find ("err-disable") == -1:		return "disabled"	if not result.find ("disabled") == -1:		return "notconnect"	if not result.find ("notconnect") == -1:		return "connected"	if not result.find ("connected") == -1:	result = ss.before	ss.expect ("[a-z0-9.-]*#")	ss.sendline ("do show interface status | include " + safePortNumber)			raise TrunkException ()	if CheckForTrunk (ss, safePortNumber):	safePortNumber = SanitisePortName (portNumber)		ss = switchSession	else:		ss = CreateSession (switchNumber)			raise NoSuchSwitchException ()		if switchNumber == -1:		switchNumber = GetSwitchNumber (switchName)	if switchSession == None:def GetConnectedStatus (switchName, portNumber, switchSession = None):## switch session.# Returns the "connected" state of the port provided. Can take a switch name or and existing## Interface function / Helper function.# GetConnectedStatus (switchName : string, portNumber : string [, switchSession : integer]) #	return False 		return True	if not ss.before.find ("portfast") == -1:	ss.expect ("[a-z0-9.-]*#")	ss.sendline ("show running-config interface " + safePortNumber)			raise TrunkException ()	if CheckForTrunk (ss, safePortNumber):	safePortNumber = SanitisePortName (portNumber)		ss = switchSession	else:		ss = CreateSession (switchNumber)			raise NoSuchSwitchException ()		if switchNumber == -1:		switchNumber = GetSwitchNumber (switchName)	if switchSession == None:def CheckForPortfast (switchName, portNumber, switchSession = None):## a switch name or an existing switch session.# Returns True if the provided port has portfasting enabled or False otherwise. Can take## Interface function / Helper function.# CheckForPortfast (switchName : string, portNumber : string [, switchSession : integer])#		return vlan		ss.expect ("[a-z0-9.-]*\(config-if\)#")	ss.sendline ("switchport access vlan " + vlan)	ss.expect ("[a-z0-9.-]*\(config-if\)#")	ss.sendline ("interface " + safePortNumber)	ss.expect ("[a-z0-9.-]*\(config\)#")	ss.sendline ("configure terminal")		raise VLANDisallowedException ()	if vlan not in ["6","11","230"]:		raise TrunkException ()	if CheckForTrunk (ss, safePortNumber):	safePortNumber = SanitisePortName (portNumber)	ss = CreateSession (switchNumber)		raise NoSuchSwitchException ()	if switchNumber == -1:	switchNumber = GetSwitchNumber (switchName)def SetVLAN (switchName, portNumber, vlan):## Changes the access VLAN on the port provided. Returns the VLAN ID if successful.## Interface function.# SetVLAN (switchName : string, portNumber : string, vlan : string)#		return safeDescription		ss.expect ("[a-z0-9.-]*\(config-if\)#")	ss.sendline ("description " + safeDescription)	ss.expect ("[a-z0-9.-]*\(config-if\)#")	ss.sendline ("interface " + safePortNumber)	ss.expect ("[a-z0-9.-]*\(config\)#")	ss.sendline ("configure terminal")		safeDescription = "".join (SanitizeDescription (description))		raise TrunkException ()	if CheckForTrunk (ss, safePortNumber):	safePortNumber = SanitisePortName (portNumber)	ss = CreateSession (switchNumber)		raise NoSuchSwitchException ()	if switchNumber == -1:	switchNumber = GetSwitchNumber (switchName)def SetDescription (switchName, portNumber, description):

UCC Webmasters
ViewVC Help
Powered by ViewVC 1.0.5