pexpect.exitstatus not working?

Laszlo Zsolt Nagy gandalf at geochemsource.com
Thu Sep 1 13:00:09 CEST 2005


Laszlo Zsolt Nagy wrote:

>This function:
>
>def scp(from_path,to_path,pwd):
>    """Copy a file with scp."""
>    cmd = '/bin/csh -c "scp -q %s %s ; echo XXX"' %(from_path,to_path)
>    print cmd
>    child = pexpect.spawn(cmd)
>    child.expect('Password:')
>    child.sendline(pwd)    
>    child.expect('XXX')
>    return child.exitstatus
>
>always returns None. 
>
....

>How can I 
>get the exit status code? Please help me.
>  
>
I could develop a workaround, but this is a real hack, using bourne 
shell and the $? variable.

def scp(from_path,to_path,pwd):
    """Copy a file with scp.
   
    Return the exit code."""
    cmd = '/bin/sh -c "scp -q %s %s ; echo $? ; echo XXX "' 
%(from_path,to_path)
    print cmd
    child = pexpect.spawn(cmd)
    child.expect('Password:')
    child.sendline(pwd)
    child.expect("XXX")
    parts = []
    for item in child.before.split('\n'):
    if item.strip() != "":
        parts.append(item.strip())
    code = int(parts[-1])
    print "exit code:", code
    if (code != 0):
    print parts[0]
    return code

Is there a platform independent solution?

   Les




More information about the Python-list mailing list