On 2011/08/25 07:51 AM, Johan Geldenhuys wrote:
Hi all,
I have the following code that uses pexpect to execute a system command. My
problem is, I don't know how to identify if the command was successfully
executed.
The code works as it is to execute the SCP command, but it executes
regardless if the SCP session can actually connect to something.
Thanks
def doScp(files):
logger.log("Files to get: " + `files`)
fNames = ' '.join(files)
cmd = 'scp %s %s@%s:%s%s' % (fNames,
SCP_USERNAME,
SCP_HOST,
SCP_IMG_FILE_DIR,
SCP_IMG_FILE_PATH)
logger.log("Sending: " + cmd)
child = pexpect.spawn(cmd)
i = child.expect(['assword:', 'yes/no'], timeout=30)
if i == 0:
child.sendline(SCP_PASSWD)
elif i == 1:
child.sendline("yes")
child.expect("assword:", timeout=30)
child.sendline(SCP_PASSWD)
data = child.read()
if data != None:
success = True
else:
success = False
child.close()
logger.log("Files sent to SCP host")
return success
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Once you call child.close() the exit and signal status will be stored in
.exitstatus and .signalstatus, for a normal exit of the program
.exitstatus will store the return code from SCP as per [1] [2] [3] and
.signalstatus will be None. If SCP was terminated with a signal then
.exitstatus will be None and .signalstatus will contain the signal
value. Info found in the docs [4].
So the changes to your code will be:
<snip>
data = child.read()
child.close()
if child.exitstatus and child.exitstatus == 0:
success = True
else:
success = False
<snip>
I'll leave putting in handling of failed error codes and abnormal
termination to you.
[1] http://support.attachmate.com/techdocs/2116.html
[2] http://support.attachmate.com/techdocs/2487.html
[3] http://support.attachmate.com/techdocs/2285.html
[4] http://pexpect.sourceforge.net/pexpect.html
--
Christian Witts
Python Developer
//
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor