pexpect exceptions

Michael Surette msurette at laframboise.net
Wed Jan 28 09:33:04 EST 2004


On Wed, 28 Jan 2004 01:21:49 -0800, Manuel de Ferran wrote:

> Michael Surette <msurette at laframboise.net> wrote in message news:<pan.2004.01.26.22.07.43.147063 at laframboise.net>...
>> I have been trying to automate the changing of passwords using python and
>> pexpect.  I wrote a script as a test and it works, except that it gives me
>> an exception when it stops running:
>> 
>> Exception exceptions.OSError: 
>> (10, 'No child processes') in <bound method spawn.__del__ of
>> <pexpect.spawn instance at 0x403d938c>> ignored
>> 
>> What is happening and how do I get rid of the exception?
>> 
>> I am running python 2.3.2 under Slackware linux 9.1 and pexpect 0.99. 
>> Here is the script:
>> 
>> #!/usr/bin/python
>> import pexpect
>> import sys
>> 
>> if len(sys.argv) != 3:
>>   print 'usage error!'
>>   raise SystemExit
>> 
>> name= sys.argv[1]
>> passwd= sys.argv[2]
>> 
>> a= pexpect.spawn('passwd %s'%name)
>> 
>> changed= False
>> while not changed:
>>   i= a.expect(['[Nn]ew password:','[Cc]hanged'])
>>   if i == 0:
>>     a.sendline(passwd)
>>   elif i == 1:
>>     changed= True
> 
> I have the same issue with the following code :
> #!/usr/bin/env python
> '''This runs "ls -l" on a remote host using SSH.
>     At the prompts enter hostname, user, and password.
> '''
> import pexpect
> import getpass
> 
> host = raw_input('Hostname: ')
> user = raw_input('User: ')
> password = getpass.getpass('Password: ')
> 
> child = pexpect.spawn("ssh -l %s %s /bin/ls -l"%(user, host))
> 
> child.expect('password:')
> child.sendline(password)
> 
> child.expect(pexpect.EOF)
> print child.pid,'middle',child.isalive()
> 
> print child.before
> 
> This is a slighty modified version of sshls.py (shipped with
> pexpect-examples). I've only added "print
> child.pid,'middle',child.isalive()"
> and I get the same exception : "exceptions.OSError: (10, 'No child
> processes')"
> 
> The weird thing I can't explain is that, I don't get the exception
> without ",child.alive()"

I have found a solution and an explanation.

Adding "a.close(False)" as a last line of my script fixes the problem.
The spawn.close() function calls os.pidwait() with your child process pid.
 In our two cases, that pid is dead and os.pidwait rightfully raises an
 exception.  Calling spawn.close() with a False argument bypasses this
 call.




More information about the Python-list mailing list