<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#ffffff" text="#000000">
    On 2011/08/25 07:51 AM, Johan Geldenhuys wrote:
    <blockquote cite="mid:007401cc62eb$19ee5c70$4dcb1550$@com.au"
      type="cite">
      <pre wrap="">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  -  <a class="moz-txt-link-abbreviated" href="mailto:Tutor@python.org">Tutor@python.org</a>
To unsubscribe or change subscription options:
<a class="moz-txt-link-freetext" href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a>


</pre>
    </blockquote>
    <br>
    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].<br>
    <br>
    So the changes to your code will be:<br>
    <br>
    &lt;snip&gt;<br>
        data = child.read()<br>
        child.close()<br>
    <br>
        if child.exitstatus and child.exitstatus == 0:<br>
            success = True<br>
        else:<br>
            success = False<br>
    &lt;snip&gt;<br>
    <br>
    I'll leave putting in handling of failed error codes and abnormal
    termination to you.<br>
    <br>
    [1] <a class="moz-txt-link-freetext" href="http://support.attachmate.com/techdocs/2116.html">http://support.attachmate.com/techdocs/2116.html</a><br>
    [2] <a class="moz-txt-link-freetext" href="http://support.attachmate.com/techdocs/2487.html">http://support.attachmate.com/techdocs/2487.html</a><br>
    [3] <a class="moz-txt-link-freetext" href="http://support.attachmate.com/techdocs/2285.html">http://support.attachmate.com/techdocs/2285.html</a><br>
    [4] <a class="moz-txt-link-freetext" href="http://pexpect.sourceforge.net/pexpect.html">http://pexpect.sourceforge.net/pexpect.html</a><br>
    <br>
    <div class="moz-signature">-- <br>
      <meta http-equiv="Content-Type" content="text/html;
        charset=windows-1252">
      <title>Email Signature</title>
      <style type="text/css">
p { font-size:8.5pt; font-family: Arial, Helvetica, sans-serif; color: #000;}
.subscribe {font-weight:bold; font-style:italic;}
.compuscan {color: #c60c30; letter-spacing:2px; text-transform:uppercase; font-weight:bold}
.green {color:#093;}
</style>
      <p>Christian Witts<br>
        Python Developer<br>
        <br>
        <em class="green"></em></p>
    </div>
  </body>
</html>