Search code examples
shellpython-2.7pexpect

pexpect responses includes echos of requests with <cr>


I have used pexpect and sendline before, but this time I am running a longer command with pipes and wild card, see below:

commandToRun='/bin/bash -c "/var/scripts/testscripts//extract -o | tail -3"'
returnedString = sendLine(commandToRun)

my class which has the sendLine function looks pretty much like this:

    self.connection = pexpect.spawn('%s %s' % (protocol, host))
    self.connection.setecho(False)
    self.connection.setwinsize(300, 300)

But when I was running the code, I saw that the returnedString not only includes the response it also includes the request as well.

So if I print returnedString, it look like this:

bin/bash -c "/var/scripts/testscripts//extract -o | tail -3"<cr>
100<cr>
102<cr>
103<cr>

Why does the response includes the request in the same buffer? I have already set setecho(False) and it does not help!

EDIT: (correct fix) I have to manually remove all from the response and remove the request as well. so setecho(False) still does nothing!


Solution

  • I found a solution to this myself. (turn off echo in response)

    commandToRun = 'bash -c "less /readfile | tail -4"'
    yourConnection.sendLine("stty -echo")
    commandResult = yourConnection.sendLine(commandToRun)
    self.sendLine("stty echo")
    

    So basically, run you command in a shell using 'bash -c' and then turn of echo in the bash.