Getting return value in os.system rsh call

Erik Max Francis max at
Thu Jan 30 03:56:38 CET 2003

Andrew wrote:

> I'm writing a test script which kicks off tests on remote machines,
> and I need to get the return code from the remote test in my main
> program.
> the relevent bit is
> os.system("rsh remote")
> How can I get the return value of

You can't directly (this is a problem with any of these kinds of
remotely executing things), what you get is the result of the rsh (or
ssh, if that were what you were using, and as an aside it would be
advisable to switch), not the command which rsh executed remotely.

Instead you need to resport to using os.popen (or one the
popen2.popen... functions), and actually having your remote script print
its result code.  Since you're using os.system, you obviously don't care
about the output, so try something like this (untested):

	result = None
	pipe = os.popen(r"rsh remote \; echo $?")
	    output = # strip off the newline
	    result = int(output)
	# now result should contain the exit code

This will only work if doesn't generate any output on its own;
if it does, then you'll either want to do pipe.readlines()[-1].strip()
(to get the last line) or, better yet, just toss its output altogether
(you obviously don't care about that, since you were using os.system
before); change the popen command to something like:

	r"rsh remote \> /dev/null 2\> /dev/null \; echo $?"

Note the backslashes are important, since

	r"rsh remote > /dev/null 2> /dev/null ; echo $?"

would mean something quite different (and exactly what you're not

 Erik Max Francis / max at /
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ The work will teach you how to do it.
\__/ (an Estonian proverb)
    Python chess module /
 A chess game adjudicator in Python.

More information about the Python-list mailing list