pexpect exceptions

M

Michael Surette

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
 
M

Manuel de Ferran

Michael Surette said:
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()"
 
M

Michael Surette

Michael Surette said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top