In my environment I need to use several different command line
interfaces on 1-2 remote machines. I could have used telnet but
decided for SSH for connection establishment (mainly to avoid
passwords in the clear).
Having used expect in tcl/tk in the past I was glad to find there is a
python implementation called pexpect (
http://www.noah.org/wiki/
Pexpect) and together with SSH I created a session library similar to
a combination of robot frameworks SSHLibrary and OperatingSystem
library plus other things.
Looking at the SSHLibary I think that robot framework could benefit
from integrating pexpect and offering more powerful keywords regarding
pattern matching. (It could well be that the SSHLibrary was influenced
by expect/pexpect but I miss important features.)
The SSHLibrary contains key words like Read Until Prompt and Read
Until Regexp but it is not possible to check for several patterns,
timeout and prompt at the same time (I haven’t tested it but I assume
starting several Read Until Regexp in a PARALLEL: construct would not
work as all Read commands would read from the same session [or is
there something like tee in RF?] and re-syncing/stopping all those
parallel Read commands would be an intersting task). And I don’t see
how to define a timeout handler (keyword) that processes a timeout.
In my implementation I have a general function on top of a pexpect/SSH
session, which checks for EOF, TIMEOUT and any number of patterns in
one call (using pexpect features) and can call different pattern
processors:
def sendCommandExpectPrompt(self, commandType, command,
errorPatterns=None,
errorHandlers=None,
newPrompt=None,
promptHandler=None,
successPatterns=None,
successHandlers=None,
eofHandler=None,
timeoutHandler=None,
additionalErrorText=None,
additionalPromptText=None,
additionalSuccessText=None,
additionalEofText=None,
additionalTimeoutText=None,
noPrompt=False, timeout=-1):
Each handler function is defined like the following
defaultSuccessHandler (sessionName can be compared to the SSHLibrary
session alias):
def defaultSuccessHandler(sessionName, commandType, command,
errorPattern, expectChild, prompt,
additionalText="", debugLevel=0):
""" Default error handler. Any error handler must return True or
False if not raising an exception.
True indicating processing went as expected."""
if debugLevel >= 50:
print "############### defaultSuccessHandler
###################"
return True
Converted to a keyword it could be something like
Execute Keyword If Pattern Found <arguments>
With <arguments> like:
EOF, eofKeyword with arguments, TIMEOUT, timeoutKeyword with
arguments, patterns1, pattgern1Keyword with arguments, patterns2,
pattern2Keyword with arguments, ...
(a separator between the keyword arguments and the next keyword would
be required, it could also be rearranged to first list all patterns
then list all handler keywords separated by something like
PATTERNPROCESSOR:).
Could that be a valuable enhancement of the SSHLibrary version?
Thanks,
Ulrich