# $Id: CliSyncApp.py,v 1.5 2002/05/12 06:18:23 dilliraj Exp $ # # @(#)CliSyncApp.py # Copyright (c) 2001-2002 Adventnet, Inc. All Rights Reserved. # Please read the associated COPYRIGHTS file for more details. # # ADVENTNET, INC. MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE # SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING # BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS # FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. ADVENTNET, INC. SHALL # NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF # USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. # # This sample application in python makes use of CLI API's to create a # Dedicated CLISession and send Synchronous CLI Commands to the Device # according to the options provided. # # Options: # [-s] - Dedicated(d) or Nondedicated(nd) session. Default is Dedicated. # [-n] - Remote port. The default is the telnet port(23). # [-cp] - Command Prompt. This is the prompt displayed by the device # for each command. Default is '$' # [-lp] - The login Prompt. This is the prompt that the device # issues for getting the username. Default is 'login:' # [-pp] - The Password Prompt. This is the prompt that is issued by # the device for getting the password. Default is 'Password:' # RemoteHost Mandatory - The host to which the session is to be established. # [-l] - One of the user name/login name present in the remotehost. # [-p] - Password for the user. # [-d] - Set Debugger.The option -d enables setDebug true. Default is false. # Command Mandatory - This represents the actual command sent to the Host. # # Example Usage: CliSyncApp localhost -l guest -p guest "ls -l" # from com.adventnet.cli.transport import TelnetProtocolOptionsImpl from org.python import core from jarray import zeros, array from java.lang import String, Exception, System import cli import ParseOptions usage = "CliSyncApp [ -n RemotePort] [ -cp CmdPrompt] [-lp LoginPrompt ] [-pp PasswordPrompt ] [-l LoginName ] [ -p Password ] RemoteHost Command" options = array(["-n", "-cp", "-lp", "-pp", "-l", "-p"], String) values = array([None,None,None,None,None,None] , String) flag = "true" try: opt = ParseOptions(sys.argv,options,values,usage) if opt.remArgs.__len__() < 2: opt.usage_error() except Exception: flag = "false" if flag=="true": sess=cli.CliSession() cliProtocolOpts = TelnetProtocolOptionsImpl() # the remote port if values[0]!=None: try: cliProtocolOpts.setRemotePort(int(values[0])) except ValueError: print "invalid port" raise ValueError, "invalid port" else: cliProtocolOpts.setRemotePort(23); # the command prompt if values[1]!=None: cliProtocolOpts.setPrompt(values[1]) else: cliProtocolOpts.setPrompt("$") # the login prompt if values[2]!=None: cliProtocolOpts.setLoginPrompt(values[2]) else: cliProtocolOpts.setLoginPrompt("login:"); # the password prompt if values[3]!=None: cliProtocolOpts.setPasswdPrompt(values[3]) else: cliProtocolOpts.setPasswdPrompt("Password:") # the login name if values[4]!=None: cliProtocolOpts.setLoginName(values[4]) # the password if values[5]!=None: cliProtocolOpts.setPassword(values[5]) # the remote host cliProtocolOpts.setRemoteHost(opt.remArgs[0]) sess.setCLIPrompt(cliProtocolOpts.getPrompt()) sess.setCliProtOpts(cliProtocolOpts) sess.openConnection() # the command to be sent System.out.println(sess.sendCommand(opt.remArgs[1])) sess.close()