Python Pexpect full output is not saved( "--More--"提示如何处理?)

标签 python python-2.7 stdout pexpect

我正在使用 Pexpect 在服务器上远程运行命令并将输出保存在文件中。但是,它不会保存整个输出,因为它因 --More-- 而被截断。有没有办法避免--More--,让整个输出保存在输出文件中?

我试过使用 child.setwinsize(1000,1000) 但它没有解决问题。

当前代码:

import pexpect
import time

child = pexpect.spawn('ssh username@ip_address')
time.sleep(1)

child.sendline('password')
time.sleep(1)

child.logfile = open("output.txt", "w")
child.sendline('command')
child.expect(pexpect.EOF)

print child.before, child.after
child.close

最佳答案

我找到了另一个答案 - 只需在实际命令之前执行以下命令即可。

terminal length 0

之后假设我输入了一些命令,如 show ip interface。然后,这将显示整个输出。您无需一次又一次地按回车键。作为,

child.sendline('terminal length 0')   
child.expect('# ')    
child.sendline('show ip interface')   #write your command here
child.expect('# ') 

关于Python Pexpect full output is not saved( "--More--"提示如何处理?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48190355/

相关文章:

bash - 在 Bash 中通过变量重定向 STDOUT

Python:新浏览器对象上的 selenium-chromedriver 错误

Linux:用列格式化流(管道)输出? (未确定的文件大小)

python - Celery Django - 自定义任务发现

Python内存缓存flush_all行为

python - Wapiti 安全工具 : Getting "Invalid Syntax Error "

python - 如何从 python 的标准输出中删除\n 和\r\n?

python - 如何更改 numpy recarray 某些列的数据类型?

python - 使用嵌入式 python 和 SimpleTemplate Engine 作为字符串传递给 template()

python - 如何将文本文件中的单词与关键字列表进行比较并根据关键字匹配运行函数?