[Netdevicelib-checkins] CVS: netdevicelib/src/netdevicelib connections.py,1.5,1.6 devices.py,1.2,1.3
Status: Alpha
Brought to you by:
bluecoat93
|
From: <net...@li...> - 2001-08-26 21:31:50
|
Update of /cvsroot/netdevicelib/netdevicelib/src/netdevicelib
In directory usw-pr-cvs1:/tmp/cvs-serv8581/netdevicelib
Modified Files:
connections.py devices.py
Log Message:
* implement TelnetConnection.enable()
* implement TelnetConnection.isEnabled()
* add virtual methods to Connection base class
* Connection destructor now closes the connection
* getConfig.py takes the enable password as a parameter
Index: connections.py
===================================================================
RCS file: /cvsroot/netdevicelib/netdevicelib/src/netdevicelib/connections.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** connections.py 2001/08/26 20:37:24 1.5
--- connections.py 2001/08/26 21:31:48 1.6
***************
*** 11,15 ****
from netdevicelib.devices import DeviceFactory
! import re, sys, telnetlib
DEBUGGING=0
--- 11,15 ----
from netdevicelib.devices import DeviceFactory
! import re, sys, telnetlib, types
DEBUGGING=0
***************
*** 17,21 ****
# ------------------------------------------------------------------------
! LoginFailedException = "Login failed. Bad username or password"
# ------------------------------------------------------------------------
--- 17,22 ----
# ------------------------------------------------------------------------
! LoginFailedException = "Login failed. Bad username or password"
! EnableFailedException = "Enable failed. Access denied"
# ------------------------------------------------------------------------
***************
*** 28,36 ****
assert inDevice != None
! self._device = inDevice
! self._isEnabled = 0
! self._isOpen = 0
! self._timeout = inTimeout
def disablePaging( self ):
""" Helper function to disable screen paging for a connection """
--- 29,75 ----
assert inDevice != None
! self._device = inDevice
! self._timeout = inTimeout
! self._isOpen = 0
! self._lastPrompt = ''
!
! def __del__( self ):
! """ Destructor """
! self.close()
!
! # Virtual methods -- must be overridden
! def open( self, inHost=None, inPort=23 ):
! """ Open the connection to the device """
! raise RuntimeException, "Unimplemented base class method called"
!
! def close( self ):
! """ Close the connection to the device """
! raise RuntimeException, "Unimplemented base class method called"
!
! def login( self, inUser=None, inPass=None ):
! """ Login to the device using a username and password """
! raise RuntimeException, "Unimplemented base class method called"
!
! def cmd( self, inCmd=None, inPrompt=None ):
! """ Run a command on the device and return the output """
! raise RuntimeException, "Unimplemented base class method called"
!
! # Base class methods -- may be overridden
! def isEnabled( self ):
! """ Returns true if the connection is in 'superuser' mode """
! return 1
!
! def enable( self, inPass=None ):
! """ Put the connection in 'superuser' mode """
! pass
!
! def disable( self ):
! """ Take the connection out of 'superuser' mode """
! pass
+ def getLastPrompt( self ):
+ """ Accessor method to get the last prompt we saw """
+ return self._lastPrompt
+
def disablePaging( self ):
""" Helper function to disable screen paging for a connection """
***************
*** 40,47 ****
""" Helper function to enable screen paging for a connection """
self.cmd( self._device.getCommand('enablePaging') )
!
def getConfig( self ):
""" Helper function to get the current config from a connection """
! if self._device._needsEnable and not self._isEnabled:
raise RuntimeError( "You must be enabled first" )
--- 79,86 ----
""" Helper function to enable screen paging for a connection """
self.cmd( self._device.getCommand('enablePaging') )
!
def getConfig( self ):
""" Helper function to get the current config from a connection """
! if self._device._needsEnable and not self.isEnabled():
raise RuntimeError( "You must be enabled first" )
***************
*** 54,58 ****
def __init__( self, inDevice=None ):
- """ Constructor """
assert inDevice != None
--- 93,96 ----
***************
*** 60,69 ****
self._conn = telnetlib.Telnet()
- def __del__( self ):
- """ Destructor - close the connection """
- self.close()
-
def open( self, inHost=None, inPort=23 ):
- """ Open the connection to the device """
assert inHost != None
--- 98,102 ----
***************
*** 73,78 ****
def close( self ):
- """ Close the connection to the device """
-
if self._isOpen:
self.enablePaging()
--- 106,109 ----
***************
*** 82,86 ****
def login( self, inUser=None, inPass=None ):
- """ Login to the device using a username and password """
assert inUser != None
assert inPass != None
--- 113,116 ----
***************
*** 135,150 ****
# Disable paging so that commands work correctly
self.disablePaging()
-
- def enable( self, inPass=None ):
- """ Go into 'superuser' mode on the device """
- assert inPass != None
- raise RuntimeError( "Enable/Disable not yet implemented" )
! def disable( self ):
! """ Get out of superuser mode """
! raise RuntimeError( "Enable/Disable not yet implemented" )
!
! def cmd( self, inCmd=None ):
! """ Run a command on the device and return the output """
assert inCmd != None
--- 165,170 ----
# Disable paging so that commands work correctly
self.disablePaging()
! def cmd( self, inCmd=None, inPrompt=None ):
assert inCmd != None
***************
*** 156,169 ****
self._conn.write( inCmd + "\n" )
! debug( "Looking for cmd prompt + (" +
! self._device.getPrompt('command') + ")" )
!
! result = self._conn.expect( [ self._device.getPrompt('command') ],
! self._timeout )
# Remove the prompt from the output and return the results
exp = re.compile( self._device.getPrompt('command') )
return exp.sub( '', result[2] )
!
# ------------------------------------------------------------------------
--- 176,223 ----
self._conn.write( inCmd + "\n" )
! if inPrompt != None:
! if type( inPrompt ) == types.ListType:
! prompts = inPrompt
! else:
! prompts = [ inPrompt ]
! else:
! prompts = [ self._device.getPrompt('command') ]
!
! debug( "Looking for cmd prompt + (" + str(prompts) + ")" )
+ result = self._conn.expect( prompts, self._timeout )
+
+ # Store the last prompt we saw
+ self._lastPrompt = result[1].group()
+
# Remove the prompt from the output and return the results
exp = re.compile( self._device.getPrompt('command') )
return exp.sub( '', result[2] )
!
! def enable( self, inPass=None ):
! assert inPass != None
! debug( "trying to enable with password %s" % inPass )
!
! # Send the enable command, expecting the password prompt
! self.cmd( self._device.getCommand('enable'),
! self._device.getPrompt('enable') )
!
! # Set the password
! self.cmd( inPass )
!
! # Make sure we got enabled
! if not self.isEnabled():
! raise EnableFailedException
!
! def disable( self ):
! raise RuntimeError( "Disable not yet implemented" )
!
! def isEnabled( self ):
! exp = re.compile( self._device.getPrompt( 'enabledIndicator' ) )
! if exp.search( self._lastPrompt ) == None:
! return 0
! else:
! return 1
!
# ------------------------------------------------------------------------
***************
*** 186,190 ****
def debug( inMessage="No Message" ):
if DEBUGGING:
! print "DEBUG: " + inMessage
if __name__ == "__main__":
--- 240,244 ----
def debug( inMessage="No Message" ):
if DEBUGGING:
! print 'DEBUG: %s' % inMessage
if __name__ == "__main__":
Index: devices.py
===================================================================
RCS file: /cvsroot/netdevicelib/netdevicelib/src/netdevicelib/devices.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** devices.py 2001/08/26 20:21:37 1.2
--- devices.py 2001/08/26 21:31:48 1.3
***************
*** 18,28 ****
self._commands = { 'disablePaging' : '',
'enablePaging' : '',
! 'getConfig' : '' }
! self._prompts = { 'login' : '',
! 'username' : '',
! 'password' : '',
! 'command' : '',
! 'enable' : '' }
def getPrompt( self, inKey=None ):
--- 18,30 ----
self._commands = { 'disablePaging' : '',
'enablePaging' : '',
! 'getConfig' : '',
! 'enable' : '' }
! self._prompts = { 'login' : '',
! 'username' : '',
! 'password' : '',
! 'command' : '',
! 'enable' : '',
! 'enabledIndicator': ''}
def getPrompt( self, inKey=None ):
***************
*** 72,81 ****
self.setCommand( 'enablePaging', 'terminal length 24' )
self.setCommand( 'getConfig', 'show running-config' )
!
self.setPrompt( 'login', '[Ll]ogin[:\s]*$' )
self.setPrompt( 'username', '[Uu]sername[:\s]*$' )
self.setPrompt( 'password', '[Pp]assw(?:or)?d[:\s]*$' )
! self.setPrompt( 'command', '[\w().-]*[\$#>]\s?(?:\(enable\))?\s*$' )
! self.setPrompt( 'enable', '[Pp]assword[:\s]*$' )
class CatOSDevice( Device ):
--- 74,85 ----
self.setCommand( 'enablePaging', 'terminal length 24' )
self.setCommand( 'getConfig', 'show running-config' )
! self.setCommand( 'enable', 'enable' )
!
self.setPrompt( 'login', '[Ll]ogin[:\s]*$' )
self.setPrompt( 'username', '[Uu]sername[:\s]*$' )
self.setPrompt( 'password', '[Pp]assw(?:or)?d[:\s]*$' )
! self.setPrompt( 'command', '\n[\w().-]*[\$#>]\s?(?:\(enable\))?\s*$' )
! self.setPrompt( 'enable', '[Pp]assword[:\s]*$' )
! self.setPrompt( 'enabledIndicator', '\#|enable' )
class CatOSDevice( Device ):
***************
*** 89,93 ****
self._setCommand( 'disablePaging', 'set length 0' )
self._setCommand( 'enablePaging', 'set length 24' )
!
class PixDevice( Device ):
def __init__( self ):
--- 93,99 ----
self._setCommand( 'disablePaging', 'set length 0' )
self._setCommand( 'enablePaging', 'set length 24' )
! self._setCommand( 'getConfig', 'write term' )
! self._setCommand( 'enable', 'enable' )
!
class PixDevice( Device ):
def __init__( self ):
***************
*** 99,104 ****
self.setCommand( 'disablePagingCmd', 'no pager' )
! self.setCommand( 'enablePagingCmd', 'pager' )
!
class DeviceFactory:
def createDevice( self, inClass=None ):
--- 105,112 ----
self.setCommand( 'disablePagingCmd', 'no pager' )
! self.setCommand( 'enablePagingCmd', 'pager' )
! self.setCommand( 'getConfig', 'write term' )
! self.setCommand( 'enable', 'enable' )
!
class DeviceFactory:
def createDevice( self, inClass=None ):
|