Вопрос

When executing the following script (simplified example):

#!/usr/bin/env python
import pexpect
spawn = pexpect.spawn('/bin/bash')
with open('/tmp/logfile.log', 'w') as f:
    spawn.logfile_read = f
    spawn.sendline('echo "Hello I am a really long command, in fact I am 100 characters long! Potato Potato Potato....."')
    spawn.sendline('exit')
    spawn.expect(pexpect.EOF)

I have trouble with line wrapping causing ^M characters appearing in the log. The pexpect logfile now literally looks like this:

bash-3.2$ echo "Hello I am a really long command, in fact I am 100 characters lo ^Mng! Potato Potato Potato....."
Hello I am a really long command, in fact I am 100 characters long! Potato Potato Potato.....
bash-3.2$ exit
exit

After 80 characters the line is wrapped by a carriage return, and I don't want that. I've tried a lot of different things to disable line-wrapping (using tput rmam, calling stty columns 1000 beforehand, using spawn.setwinsize(1000, 1000), setting os.environ['COLUMNS'] = "1000", passing various args to /bin/bash..) and I can't seem to find the right trick.

How can the line wrapping be disabled in this scenario?

Это было полезно?

Решение

I finally figured this out. What pointed me in the right direction was when I remembered that the echo command is internal to bash. Changing the command to /bin/echo completely changed the behavior. This was only part of the solution.

Seeing that it appears that bash itself is the problem I looked through the man page to see what the cause may be. Then it hit me! Bash uses readline support and sure enough disabling it fixes your problem regardless of using /bin/echo or the internal echo command.

Just change your spawn line to spawn = pexpect.spawn('/bin/bash --noediting').

Другие советы

I was having this same issue. Using setwinsize(1000,1000) worked for me.

child = pexpect.spawn('ssh %s@%s' % (user, host))
child.setwinsize(1000,1000)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top