【问题标题】:Issue with pexpect scriptpexpect 脚本的问题
【发布时间】:2016-03-23 00:54:35
【问题描述】:

我正在使用 pexpect 脚本来建立 ftp 连接,我遵循以下方法:

#!/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()

这很好地建立了连接,但是当我将以下行添加到脚本时出现问题

child.expect ('ftp> ', )

如果我添加此行,脚本不会执行任何操作,我不知道这是什么原因,我将不胜感激任何有关此问题的帮助。

【问题讨论】:

  • 因为它不匹配,无论出于何种原因。
  • 为什么不发布客户端进程的屏幕截图、日志或实际输出?没有这些东西,没有人可以帮助你。
  • 尝试直接创建 ftp 连接并检查是否一切正常。可能是用户名或密码错误或其他原因

标签: pexpect


【解决方案1】:

如果你打电话给child.interact(),那么你之后就不需要打电话给child.expect()。 (假设你之后调用它)。

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 ^]

您希望脚本中出现 ftp 提示符 ftp>。它会在交互调用之后自动出现。如果您在调用交互之前调用child.expect('ftp>'),则点击返回将获得所需的提示。然而,在这种情况下,期望 'ftp>' 已过时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多