python subprocess - get exit status of command run on different machine using rsh

222 views Asked by At

I am trying to run some commands on some machine which I am connecting through the rsh command (can't use ssh for my case).

Through subprocess.check_call function, I am executing the command and am expecting check_call to exit with exception when exit status of my "cd /libs/ " command indicates failure.

Below is the code I am trying out.

with open('logs.txt','w') as outstream:
    subprocess.check_call("rsh bldr 'cd /libs/' ", shell=True,stdout=outstream)

But here, rsh only returns its exit status and not the command it's executing.

Because of which even in command failure case no error from check_call function, as it is getting rsh exit status which will be 0 always.

So, how to get the exit status of the command here?

Also, how can I make check_call fail based on my command exit status?

Note: I am trying to change directory to /libs which doesn't exist. I am hoping for check_call to fail here and stop execution of script.

1

There are 1 answers

0
Ajay Srivastava On BEST ANSWER

Capture stderr as well which can be used for checking if command has failed on remote machine.

with open('logs.txt','w') as outstream:
    with open('error.txt','w') as errstream:
        ret = subprocess.check_call("rsh centos lsh", shell=True, stderr=errstream, stdout=outstream)

fpstat = os.stat('error.txt')
if fpstat.st_size > 0:
    print("problem in command on remote machine.")
else:
    print("Successful.")