使用Pexpect远程执行命令时--More--截断输出的解决方法
Hey there! The --More-- prompt you're seeing is almost certainly from a pager (like more or less) being triggered by the command you're running—either the command itself uses a pager by default, or your remote shell is configured to pipe long output through one. Let's go through a few reliable fixes to get the full output saved to your file:
1. Modify the Command to Disable Paging (Most Reliable)
The simplest fix is to bypass the pager entirely by adjusting your command. Here are common approaches:
- For commands that support a "no pager" flag (like
ls,git log), use the built-in parameter:command --no-pager - For manual pages, force output to
catinstead of a pager:man command -P cat - Universal workaround: Pipe the command's output directly to
cat, which will print everything without paging:command | cat
Update your code with this approach:
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") # Add | cat to disable paging child.sendline('command | cat') child.expect(pexpect.EOF) print(child.before, child.after) child.close()
2. Automatically Respond to --More-- Prompts
If you can't modify the command (e.g., it's a custom script that hardcodes paging), you can have Pexpect automatically handle the pagination prompts. When it sees --More--, send a space to scroll to the next page until it reaches the end of the output:
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') # Loop to handle pagination until EOF while True: try: # Match either --More-- prompt or end of output index = child.expect(['--More--', pexpect.EOF]) if index == 0: # Send space to scroll the next page child.send(' ') else: # Reached end of output, exit loop break except pexpect.TIMEOUT: # Handle timeout (adjust or remove based on your needs) break print(child.before, child.after) child.close()
Note: Some systems show --More-- with extra text (like --More--(75%)), so you might need to use a regex match like r'--More--.*' if the basic string doesn't work.
3. Override the Default Pager via Environment Variables
You can tell the remote shell to use cat as the default pager instead of more or less, which will prevent pagination for most commands. Set the PAGER variable before running your command:
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") # Set default pager to cat child.sendline('export PAGER=cat') time.sleep(0.5) child.sendline('command') child.expect(pexpect.EOF) print(child.before, child.after) child.close()
This works for most commands that respect the PAGER variable, but note that some tools may hardcode their pager choice and ignore this setting.
Why setwinsize Didn't Work
The setwinsize(1000,1000) trick often fails because many pagers check the length of the output itself instead of the terminal window size, or the remote server doesn't pick up the terminal size change from Pexpect reliably. The fixes above target the root cause of the paging rather than trying to work around terminal dimensions.
内容的提问来源于stack exchange,提问作者markford

