Problem with PEXPECT in Python

K

Kevin Erickson

Hello,

I am attempting to use pexpect in python to copy files from a server
using scp; the copy works however exceptions are thrown and it exits
unsuccessfully. Below is the a sample code and the error:

#Begin Code

import sys
import pexpect

def GetServerData(self):
foo = pexpect.spawn('scp (e-mail address removed):/home/config/
role{/file1,/files/file2,/files/file3} /tmp')
foo.expect('.*password:*')
foo.sendline('server_password')
foo.interact()

#End Code

#Begin Output

file1 100% 10KB 10.0KB/s
00:01
file2 100% 946 0.9KB/s
00:00
file3 100% 1655 1.6KB/s
00:00
Traceback (most recent call last):
File "mygui.py", line 6, in ?
f = Form1()
File "/home/Kevin/form1.py", line 41, in __init__
self.GetServerData()
File "/home/Kevin/form1.py", line 84, in GetServerData
foo.interact()
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1062, in
interact
selfw.__interact_copy(escape_character)
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1086, in
__interact_copy
data = self.__interact_read(self.child_fd)
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1075, in
__interact_read
return os.read(fd, 1000)
OSError: [Errno 5] Input/output error

#End Output

The problem seems to be with the call foo.interact(); does anyone know
how to fix this? Any help will be much appreciated.
 
K

Kevin Erickson

Hello,

I am attempting to use pexpect in python to copy files from a server
using scp; the copy works however exceptions are thrown and it exits
unsuccessfully. Below is the a sample code and the error:

#Begin Code

import sys
import pexpect

def GetServerData(self):
foo = pexpect.spawn('scp (e-mail address removed):/home/config/
role{/file1,/files/file2,/files/file3} /tmp')
foo.expect('.*password:*')
foo.sendline('server_password')
foo.interact()

#End Code

#Begin Output

file1 100% 10KB 10.0KB/s
00:01
file2 100% 946 0.9KB/s
00:00
file3 100% 1655 1.6KB/s
00:00
Traceback (most recent call last):
File "mygui.py", line 6, in ?
f = Form1()
File "/home/Kevin/form1.py", line 41, in __init__
self.GetServerData()
File "/home/Kevin/form1.py", line 84, in GetServerData
foo.interact()
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1062, in
interact
selfw.__interact_copy(escape_character)
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1086, in
__interact_copy
data = self.__interact_read(self.child_fd)
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1075, in
__interact_read
return os.read(fd, 1000)
OSError: [Errno 5] Input/output error

#End Output

The problem seems to be with the call foo.interact(); does anyone know
how to fix this? Any help will be much appreciated.


I have found a work around for my problem. I replace the following
line:

foo.interact()
with
foo.expect(pexpect.EOF)

That change gets rid of the errors and works great for copying the
files down from the server.

Kevin
 
N

Noah

Kevin said:
....
I have found a work around for my problem. I replace the following line:

foo.interact()
with
foo.expect(pexpect.EOF)

That is correct. But why did you try using interact() method in the
first place?
I just want to know to see if I could improve the documentation.
After you send the password the 'scp' command should finish quickly
and exit,
so there would be nothing for a human to interact with.

You could also try using the run() function which is a simplified
interface to pexpect.
Something like this might work:
pexpect.run ('scp (e-mail address removed):/home/config/role{/file1,/files/
file2,/files/file3} /tmp',
events={'(?i)password': 'server_password'})
That's all there is to it. The run() function will run the given scp
command.
When it see 'password' in the output it will send the server_password.

Yours,
Noah


Yours,
Noah
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top