Tips and Traps¶
pexpect
.spawn .expect takes a pattern parameter. The pattern can be a StringType, EOF, a compiled re, or a list of any of those types. Notice that strings will be compiled to retypes, so make sure to use valid regular expression patterns if you provide strings. For example, if password is asked after the prompt "password for user (not stored): ", passing the literal string"password for user (not stored): "won’t work. You have to use something like"password for user \(not stored\): "or a simpler one"password for user"(if no ambiguity).The command-line tool of some (e.g., network) applications might be slow to authenticate. If you use pexect to automate such a command-line tool, it is best to wait for sometime after sending password using
child.sendline(passwd). If the authentication has ouput on both success and failure, a smart way is to wait for the success or failure message to come out.child.expect(".*success_pattern.*|.*failure_pattern.*")If the the output of authentication contains the same keyword (e.g., dclong) on both success and failure, the pattern can be further simplified.
child.expect(".*dclong.*")The method
spawn.interactgives control of the child process back to the interactive user (the human at the keyboard). Keystrokes are sent to the child process, and the stdout and stderr output of the child process is printed. This is extremely useful if you only wantpexpectto hijack the child process for certain interactions. For example, if a command-line tool requires both a password and a hardware token for authentication, you can usepexpectto hijack the child process to enter password automatically and then give control of the child process back to the user to authenticate using hard token.References¶