0

我以前使用过pexpectand sendline,但这次我使用管道通配符运行更长的命令,见下文:

commandToRun='/bin/bash -c "/var/scripts/testscripts//extract -o | tail -3"'
returnedString = sendLine(commandToRun)

我的具有 sendLine 函数的类看起来很像这样:

    self.connection = pexpect.spawn('%s %s' % (protocol, host))
    self.connection.setecho(False)
    self.connection.setwinsize(300, 300)

但是当我运行代码时,我看到它returnedString不仅包括响应,还包括请求。

所以如果我打印returnedString,它看起来像这样:

bin/bash -c "/var/scripts/testscripts//extract -o | tail -3"<cr>
100<cr>
102<cr>
103<cr>

为什么响应将请求包含在同一个缓冲区中? 我已经设置setecho(False)了,它没有帮助!

编辑:(正确修复)我必须手动从响应中删除所有内容并删除请求。所以 setecho(False) 仍然什么都不做!

4

1 回答 1

1

我自己找到了解决方案。(关闭回声作为回应)

commandToRun = 'bash -c "less /readfile | tail -4"'
yourConnection.sendLine("stty -echo")
commandResult = yourConnection.sendLine(commandToRun)
self.sendLine("stty echo")

所以基本上,使用' bash -c'在shell中运行你的命令,然后echo在bash中打开。

于 2012-11-30T10:16:57.337 回答