Reputation: 1499
I am using pexpect to run a series of commands to a remote machine and would like to get the exit status of each command. However, when I try to retrieve the exit status I am getting the incorrect value. Is there a way to get the correct exit status using pxssh from Pexpect? Here is the code I have tried along with the output. The command was successful but the exit status given was 255 instead of 0.
from pexpect import pxssh
try:
s = pxssh.pxssh(timeout=30, maxread=2000000, options={
"StrictHostKeyChecking": "no",
"UserKnownHostsFile": "/dev/null"})
hostname = 'my_hostname'
username = 'username'
password = 'my_pass'
s.login (hostname, username, password, port=22, auto_prompt_reset=False)
s.PROMPT = '*$'
s.sendline('uptime')
s.prompt()
print(s.before.decode('utf-8'))
s.close()
print('s exitstatus=', s.exitstatus)
except pxssh.ExceptionPxssh as e:
print("pxssh failed on login.")
print(e)
Output:
uptime
09:10:39 up 10 days, 17:35, 4 users, load average: 0.12, 0.18, 0.16
s exitstatus= 255
Upvotes: 1
Views: 2145
Reputation: 20797
.exitstatus
is the exit status of the ssh
process. See following example to see how to get the exit status of a command run in the ssh session:
>>> ssh = pxssh.pxssh()
>>> ssh.login('127.0.0.1', 'root', 'passwd')
True
>>> ssh.sendline('ls not-found')
13
>>> ssh.prompt()
True
>>> ssh.before
'ls not-found\r\nls: cannot access not-found: No such file or directory\r\n'
>>> ssh.sendline('echo $?')
8
>>> ssh.prompt()
True
>>> ssh.before
'echo $?\r\n2\r\n'
>>> ssh.before.split('\r\n')[1]
'2' <-- This is the ls command's exit status
>>>
Upvotes: 3