0

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!

theAlse
  • 5,577
  • 11
  • 68
  • 110
  • what is `sendLine()`? spawn.sendline() method returns number of bytes written – jfs Nov 01 '12 at 13:30
  • @J.F.Sebastian, I edited the code above! – theAlse Nov 01 '12 at 14:05
  • could you provide a minimal (it executes just one command) complete (it can be run standalone) example that reproduces your problem. – jfs Nov 01 '12 at 15:18
  • @J.F.Sebastian, that would be hard since I am running the command on a machine I can not share. The question is simple? why is request echoed in the response? and why does it include carrier return-charecter in the echo? – theAlse Nov 01 '12 at 21:40
  • The problem (most probably) is in your code, so showing it might help. You don't need to share any machine. Does the problem persist with `"/bin/bash -c 'ls | tail -3'"` command? Do you know the difference between `.logfile_read`/`_send`? What is protocol? Where is the code that returns the string? – jfs Nov 02 '12 at 02:00
  • 1
    Hey there, I am facing the same problem as you.... Found any answer to this? – amulllb Nov 19 '12 at 22:58
  • @abarik, Unfortunately I have not found a real solution to this problem. What I have done is instead to remove all < CR> and from the response first and then remove the request from the response as well. then finally you are left only with the response! – theAlse Nov 20 '12 at 09:45
  • @theAlse, Thanks... I actually never faced a problem with getting in the output, but faced a problem where I thought my setecho=False doesn't seem to work.... for me, the cmd (sendline) would appear in the output (print self.connection.before). All I did was strip it off just like you are doing... Any case, it would be good to know that even though setecho doesn't work, but in case you are logging, use logfile_read, instead of logfile (http://code.google.com/p/cisco-ios-cli-automation/) – amulllb Nov 25 '12 at 05:05

1 Answers1

1

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.

theAlse
  • 5,577
  • 11
  • 68
  • 110