Problem with PEXPECT in Python

Noah noah at noah.org
Mon Jul 2 12:38:19 EDT 2007


Kevin Erickson wrote:
> On Jun 30, 5:50 pm, Kevin  Erickson <kevin.erick... at gmail.com> wrote:
> > #Begin Code
> >
> > import sys
> > import pexpect
> >
> > def GetServerData(self):
> >             foo = pexpect.spawn('scp u... at server.com:/home/config/
> > role{/file1,/files/file2,/files/file3} /tmp')
> >             foo.expect('.*password:*')
> >             foo.sendline('server_password')
> >             foo.interact()
...
> 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 u... at server.com:/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




More information about the Python-list mailing list