[Tutor] Confirmation if command worked

Johan Geldenhuys johan at accesstel.com.au
Thu Aug 25 07:51:55 CEST 2011


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







More information about the Tutor mailing list