I'm trying to execute a pexpect spawn command as sudo and getting timeout error
import pexpect,os,commands,getpass
child = pexpect.spawn ('su - oracle -c "/home/Middleware/bin/emctl status oms -details"')
child.expect("Enter Enterprise Manager Root (SYSMAN) Password :")
child.sendline("welcome1")
child.expect(pexpect.EOF, timeout=None)
cmd_show_data = child.before
cmd_output = cmd_show_data.split('\r\n')
for data in cmd_output:
print data
Below is the execution output:
pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().
<pexpect.spawn object at 0x9ae510>
version: 2.3 ($Revision: 399 $)
command: /bin/su
args: ['/bin/su', '-', 'oracle', '-c', '/home/Middleware/bin/emctl status oms -details']
searcher: searcher_re:
0: re.compile("Enter Enterprise Manager Root (SYSMAN) Password :")
buffer (last 100 chars): , 2016 Oracle Corporation. All rights reserved.
Enter Enterprise Manager Root (SYSMAN) Password :
before (last 100 chars): , 2016 Oracle Corporation. All rights reserved.
Enter Enterprise Manager Root (SYSMAN) Password :
after: <class 'pexpect.TIMEOUT'>
It's because your search string doesn't match the output when compiled to a Regular Expression.
As per the spawn.expect
documentation:
The pattern can be a StringType, EOF, a compiled re, or a list of any of those types. Strings will be compiled to re types.
The problem is the parenthesis, they are special characters in Regular Expressions and must be escaped with a backslash when intended to be treated as literals.
print(re.match("Enter Enterprise Manager Root (SYSMAN) Password :",
"Enter Enterprise Manager Root (SYSMAN) Password :"))
# Prints: None
print(re.match("Enter Enterprise Manager Root \(SYSMAN\) Password :",
"Enter Enterprise Manager Root (SYSMAN) Password :"))
# Prints: <_sre.SRE_Match object; span=(0, 49), match='Enter Enterprise Manager Root (SYSMAN) Password :>