2

我正在尝试在 winpexpect 中使用 plink 连接到远程 Linux 服务器。我正在使用以下代码:

child = winpexpect.winspawn('plink root@hostname')
child.logfile = sys.stdout
i = child.expect(['Password:')
child.expect('Password:')
child.sendline('password')

我在标准输出上得到的输出是:

Using keyboard-interactive authentication.
Password: password

Using keyboard-interactive authentication.
Password:
Using keyboard-interactive authentication.
Password: Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  ...in expect_loop
raise TIMEOUT (str(e) + '\n' + str(self))
pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().
...
command: plink
args: ['plink', 'root@hostname']
buffer (last 100 chars): yboard-interactive authentication.
Password:
Using keyboard-interactive authentication.
Password:
before (last 100 chars): yboard-interactive authentication.
Password:
Using keyboard-interactive authentication.
Password:
after: <class 'pexpect.TIMEOUT'>
...

等效代码在Linux下的pexpect中工作(用pexpect替换winpexpect模块,用ssh替换plink调用),所以我知道expect()匹配是正确的。看起来 winpexpect 正在写入屏幕,而 plink 没有将其注册为输入密码字段的文本。

任何人都可以在这里发现问题吗?

4

2 回答 2

2

这是一个带有错误修复的分叉 winexpect 项目:https ://bitbucket.org/weyou/winpexpect 你可以试试。

于 2012-09-07T08:48:52.873 回答
0

我遇到了与 winpexpect 和 plink 类似的问题。这个问题有一个对我有用的解决方案。连接到我的串行接口的设备在本地回显内容并使 winpexpect 感到困惑。将 sendline() 替换为执行以下操作的函数:

    child.send(cmd)
    child.pexpect(cmd)
    child.send('\n')

每个电话都为我修复了它。child.pexpect(cmd) 在目标种子 \n 之前吃掉本地回显的字符并开始响应。

于 2015-03-29T22:46:42.423 回答