[Netdevicelib-checkins] CVS: netdevicelib/src/netdevicelib connections.py,1.8,1.9
Status: Alpha
Brought to you by:
bluecoat93
From: <net...@li...> - 2001-10-04 18:12:36
|
Update of /cvsroot/netdevicelib/netdevicelib/src/netdevicelib In directory usw-pr-cvs1:/tmp/cvs-serv24315/src/netdevicelib Modified Files: connections.py Log Message: - move debug logging into the Connection class -- debug() and _debuglog() - add command-line processing to the __main__ test driver Index: connections.py =================================================================== RCS file: /cvsroot/netdevicelib/netdevicelib/src/netdevicelib/connections.py,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** connections.py 2001/09/07 01:37:13 1.8 --- connections.py 2001/10/04 18:12:33 1.9 *************** *** 11,17 **** from netdevicelib.devices import DeviceFactory ! import re, sys, telnetlib, types ! ! DEBUGGING=0 # ------------------------------------------------------------------------ --- 11,15 ---- from netdevicelib.devices import DeviceFactory ! import getopt, re, sys, telnetlib, types # ------------------------------------------------------------------------ *************** *** 29,37 **** """ Constructor """ assert inDevice != None ! ! self._device = inDevice ! self._timeout = inTimeout ! self._isOpen = 0 ! self._lastPrompt = '' # Virtual methods -- must be overridden --- 27,36 ---- """ Constructor """ assert inDevice != None ! ! self._device = inDevice ! self._timeout = inTimeout ! self._isDebugging = 0; ! self._isOpen = 0 ! self._lastPrompt = '' # Virtual methods -- must be overridden *************** *** 53,56 **** --- 52,69 ---- # Base class methods -- may be overridden + def _debuglog( self, inMessage="No Message" ): + """ Write a debug message to STDERR if debugging is enabled """ + + if self.debug(): + sys.stderr.write( "DEBUG: %s\n" % inMessage ) + + def debug( self, inLevel=None ): + """ Accessor method for the debugging flag """ + + if inLevel != None: + self._isDebugging = inLevel + + return self._isDebugging + def isEnabled( self ): """ Returns true if the connection is in 'superuser' mode """ *************** *** 102,106 **** self._conn.open( inHost ) self._isOpen = 1 ! debug( "Connection open" ) def close( self ): --- 115,119 ---- self._conn.open( inHost ) self._isOpen = 1 ! self._debuglog( "Connection open" ) def close( self ): *************** *** 123,155 **** self._device.getPrompt('password') ] ! debug( "Looking for username prompt (" + ! self._device.getPrompt( 'username' ) + ")" ) result = self._conn.expect( matches, self._timeout ) ! debug( "Found username prompt" ) # Unless we only got a password prompt, send the username if result[0] != 2: # Send the login name ! debug( "Sending username (" + inUser + ")" ) self._conn.write( inUser + "\n" ) # Wait for password prompt ! debug( "Looking for password prompt (" + ! self._device.getPrompt( 'password' ) + ")" ) result = self._conn.expect( [ self._device.getPrompt('password') ], self._timeout ) ! debug( "Found password prompt" ) # Send password ! debug( "Sending password (" + inPass + ")" ) self._conn.write( inPass + "\n" ) # Wait for command prompt or another login prompt ! debug( "Looking for cmd prompt + (" + ! self._device.getPrompt('command') + ")" ) result = self._conn.expect( matches + --- 136,168 ---- self._device.getPrompt('password') ] ! self._debuglog( "Looking for username prompt (" + ! self._device.getPrompt( 'username' ) + ")" ) result = self._conn.expect( matches, self._timeout ) ! self._debuglog( "Found username prompt" ) # Unless we only got a password prompt, send the username if result[0] != 2: # Send the login name ! self._debuglog( "Sending username (" + inUser + ")" ) self._conn.write( inUser + "\n" ) # Wait for password prompt ! self._debuglog( "Looking for password prompt (" + ! self._device.getPrompt( 'password' ) + ")" ) result = self._conn.expect( [ self._device.getPrompt('password') ], self._timeout ) ! self._debuglog( "Found password prompt" ) # Send password ! self._debuglog( "Sending password (" + inPass + ")" ) self._conn.write( inPass + "\n" ) # Wait for command prompt or another login prompt ! self._debuglog( "Looking for cmd prompt + (" + ! self._device.getPrompt('command') + ")" ) result = self._conn.expect( matches + *************** *** 157,161 **** self._timeout ) ! debug( "Found cmd prompt" ) # Bad login if we got another login prompt --- 170,174 ---- self._timeout ) ! self._debuglog( "Found cmd prompt" ) # Bad login if we got another login prompt *************** *** 163,167 **** raise LoginFailedException else: ! debug( "login succeeded" ) # Disable paging so that commands work correctly --- 176,180 ---- raise LoginFailedException else: ! self._debuglog( "login succeeded" ) # Disable paging so that commands work correctly *************** *** 176,180 **** return "" ! debug( "running command (" + inCmd + ")" ) self._conn.write( inCmd + "\n" ) --- 189,193 ---- return "" ! self._debuglog( "running command (" + inCmd + ")" ) self._conn.write( inCmd + "\n" ) *************** *** 187,191 **** prompts = [ self._device.getPrompt('command') ] ! debug( "Looking for cmd prompt + (" + str(prompts) + ")" ) result = self._conn.expect( prompts, self._timeout ) --- 200,204 ---- prompts = [ self._device.getPrompt('command') ] ! self._debuglog( "Looking for cmd prompt + (" + str(prompts) + ")" ) result = self._conn.expect( prompts, self._timeout ) *************** *** 206,210 **** assert inPass != None ! debug( "trying to enable with password %s" % inPass ) # Send the enable command, expecting the password prompt --- 219,223 ---- assert inPass != None ! self._debuglog( "trying to enable with password %s" % inPass ) # Send the enable command, expecting the password prompt *************** *** 240,243 **** --- 253,258 ---- class ConnectionFactory: + """ Factory class for creating Connecton sub-class objects """ + def createConnection( self, inType=None, inClass=None ): """ Factory method to create Connection sub-class objects """ *************** *** 253,279 **** raise RuntimeError( "Type '" + inType + "' not supported" ) ! # ------------------------------------------------------------------------ - def debug( inMessage="No Message" ): - if DEBUGGING: - print 'DEBUG: %s' % inMessage - if __name__ == "__main__": ! if len( sys.argv ) < 5: print "usage: connections.py " + \ "hostname username password [enable] command" sys.exit(1) ! conn = ConnectionFactory().createConnection( "telnet", "IOS" ) - conn.open( sys.argv[1] ) - conn.login( sys.argv[2], sys.argv[3] ) ! if len( sys.argv ) == 6: ! conn.enable( sys.argv[4] ) ! cmd = sys.argv[5] else: ! cmd = sys.argv[4] ! lines = conn.cmd( cmd ) print lines conn.close() --- 268,316 ---- raise RuntimeError( "Type '" + inType + "' not supported" ) ! # ======================================================================== ! # Test driver ! # ======================================================================== if __name__ == "__main__": ! ! # Parse our command-line arguments ! debugging = 0 ! try: ! opts, args = getopt.getopt( sys.argv[1:], "d", ["debug"] ) ! except getopt.GetoptError: ! print "Use -d or --debug for debugging" ! sys.exit(1) ! for o,a in opts: ! if o in ( '-d', '--debug' ): ! debugging = 1 ! ! # Make sure they entered all the parameters we need ! if len( args ) < 4: print "usage: connections.py " + \ "hostname username password [enable] command" sys.exit(1) ! ! # Create the connection object conn = ConnectionFactory().createConnection( "telnet", "IOS" ) ! # Set the debugging flag if the user indicated debugging ! if debugging: ! conn.debug(1) ! ! # Open the connection and login ! conn.open( args[0] ) ! conn.login( args[1], args[2] ) ! ! # If the user provided an enable password, go into enable mode ! if len( args ) == 6: ! conn.enable( args[3] ) ! cmd = args[4] else: ! cmd = args[3] ! ! # Run the command and print the output lines = conn.cmd( cmd ) print lines + + # Finally, close the connection conn.close() |