Search code examples
pexpect

pexpect command output truncated by columns


The output of command systemctl status dnsmasq with pexpect.spawn() gets truncated.

pexpect.spawn('systemctl status dnsmasq', encoding='utf-8')
i = child.expect([".*\$ ", pexpect.EOF, pexpect.TIMEOUT])
if i == 1:
    child.sendcontrol("c")
print(child.before)

See image of output below:

systemctl status dnsmasq

I tested this for sshd.service and result is same. However for other commands like sudo apt update and sudo apt upgrade, pexpect logs full output. What's causing this behavior and how to correct it?


Solution

  • By default the pty size is 24x80. Some programs would adjust their output format based on the pty size. You can explicitly set the pty size when calling spawn().

    child = pexpect.spawn('systemctl status dnsmasq', encoding='utf-8',
                          dimensions=(9999, 9999) )
                          #           rows, cols