SAFplus Runtime Director API  3.0.5
pcli.Pcli Class Reference

This class encapsulates the basic idea of issuing commands to a CLI. More...

Inheritance diagram for pcli.Pcli:
Collaboration diagram for pcli.Pcli:

Public Member Functions

def __init__
 Optional parameter specifies the return code on success for use in detecting if a command succeeded or not. More...
 
def underlying_expect
 Return the raw expect object so you can do your own expecting. More...
 
def set_prompt
 
def get_return_code
 Override this function to implement the logic that gets a result code from the execution of the prior command. More...
 
def prompt
 This matches the shell prompt. More...
 
def run
 Wrapper for run_raw that raises an exception if the return code is not 0. More...
 
def run_raw
 Run an arbitrary command, with timeout. More...
 

Public Attributes

 success_return_code
 
 exp
 
 promptText
 
 rePROMPT
 
 default_timeout
 

Detailed Description

This class encapsulates the basic idea of issuing commands to a CLI.

Constructor & Destructor Documentation

def pcli.Pcli.__init__ (   self,
  exp,
  prompt,
  success_return_code = None 
)

Optional parameter specifies the return code on success for use in detecting if a command succeeded or not.

You may also just set the self.success_return_code variable.

Member Function Documentation

def pcli.Pcli.get_return_code (   self,
  output 
)

Override this function to implement the logic that gets a result code from the execution of the prior command.

The run_raw command will call this function with the output of the command (so you can parse it if the return code is in the output). Or you can "expect" for the code

def pcli.Pcli.prompt (   self,
  timeout = 20 
)

This matches the shell prompt.

This is little more than a short-cut to the expect() method. This returns True if the shell prompt was matched. This returns False if there was a timeout. Note that if you called login() with auto_prompt_reset set to False then you should have manually set the PROMPT attribute to a regex pattern for matching the prompt.

def pcli.Pcli.run (   self,
  cmd,
  stim_resp_spec = {},
  timeout = None 
)

Wrapper for run_raw that raises an exception if the return code is not 0.

def pcli.Pcli.run_raw (   self,
  cmd,
  stim_resp_spec = None,
  timeout = None 
)

Run an arbitrary command, with timeout.

Match the output against user defined stimuli & make a variety of responses.

Returns a tuple (int(return code), "output").

The timeout is the maximum time to wait before any matches to the stim_resp_spec occur, not the total maximum time to wait.

The stim_resp_spec is typically used to answer any questions or prompts that running the command generates. It is not intended to pick relevant information from the command's output. That is best done by parsing the output of run_raw yourself.

The format of the stim_resp_spec allows for very powerful and succinct specifications of questions and answers. It is best learned by example, see tests/test_psh.py.

Basically the spec is a nested set of dictionaries and lists (or tuples). If a dictionary is encountered, the routine 'expects' for any key (string or regular expression) in the list, and then executes the 'value'. What executing the 'value' means depends on the type of the 'value': None: Don't send anything, but reset match activity timeout string: 'send' the string (don't forget to put
at the end)! dictionary: 'expect' for any key (recursively) list: execute the items in the list in order as 'values' tuple: same as list function: define as fn(rec,key_or_match). Executes the function, passing as the first parameter a function for you to call to recurse, and the second the key that was matched or the 'match' object if the key was a regular expression. The return value of this function will be executed as a 'value', so should be a type as described here. Exception: raises the exception int: aborts and returns this as the return code (ignores the actual return code) PopN(x) Special item meaning 'finish x levels of recursion' Return(x) Special item meaning is the same as an int, but x can be any type.

Examples: sh.run_raw("echo q1:; read a1; echo q2:; read a2; echo answers: $a1 $a2", { "q1" : "r1\n", "q2" : "r2\n" }, 5)

(ret, output) = self.run("scp -q %s %s" % (frm, to), {'assword': 's
' % password, '(yes/no)?':'yes
'}, 15)

def pcli.Pcli.set_prompt (   self,
  prompt 
)
def pcli.Pcli.underlying_expect ( )

Return the raw expect object so you can do your own expecting.

Member Data Documentation

pcli.Pcli.default_timeout
pcli.Pcli.exp
pcli.Pcli.promptText
pcli.Pcli.rePROMPT
pcli.Pcli.success_return_code