Skip to main content
 首页 » 编程设计

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

2024年09月07日7mfrbuaa

我正在使用 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('# ')