Search code examples
pexpect

Issue with pexpect script


I was working in a pexpect script in order to establish a ftp connection, I followed the following approach:

#!/usr/bin/python
import pexpect
child = pexpect.spawn ('ftp mydomanin.com.mx')
child.expect ('.*[Nn]ame.*: ')
child.sendline ('user')
child.expect ('Password:')
child.sendline ('mypassword')
child.interact()

This works well the connection is establish, but the problem comes when I add the following line to the script

child.expect ('ftp> ', )

The script doesn't do nothing if I add this line, I don't know what is the reason for this i would appreciate any help whit this issue.


Solution

  • If you are calling child.interact() then you need not call child.expect() afterwards. (Assuming you are calling it afterwards).

    Interact gives control of the child process to the interactive user (the human at the keyboard). Keystrokes are sent to the child process, and the stdout and stderr output of the child process is printed. This simply echos the child stdout and child stderr to the real stdout and it echos the real stdin to the child stdin. When the user types the escape_character this method will stop. The default for escape_character is ^]

    You are expecting ftp prompt ftp> from your script. It will automatically appear after the interact call. If you are calling child.expect('ftp>') before the call to interact then hitting return will get the desired prompt. Expecting 'ftp>' is obsolete in this case however.