Pexpect: Read from the last send
<p>I am trying to read the output of pexpect.send(cmd) but here's the problem I am facing.</p>

<p>I am sending many commands in a sequence and I want to read/expect after a certain set of commands. Condition is that only the output of last command is to be considered. But <code>expect</code> matches from the point it last read. I have tried different methods such as matching for an EOF before sending the command of which I need the output but EOF means that child has terminated. I have tried reading till timeout and then sending the command but timeout itself causes the child to terminate. </p>

<p>I have looked for ways in which I could read from the end or the last line of output. I am considering reading a fixed bytes to a file or string and then manipulate the output to get the info I want. Here as well the fixed number of bytes is not fixed. There does not seems to be a reliable way to do this.</p>

<p>Could anyone help me sort this out ?</p>

<p>There are three ways in which this problem can be handled but none to flush the buffer</p>

<ol>
<li>In Pexpect every <code>send</code> call should be matched with a call to
<code>expect</code>. This ensures that the file pointer has moved ahead of the
previous send.</li>
<li>If there is a series of <code>send</code> before a single <code>expect</code> then we need
to provide a way to move file pointer to the location of last send.
This can be done by an extra <code>send</code> whose <code>expect</code> output is unique.
The uniqueness should be such that none of the <code>send</code> in the series
of <code>send</code> should give that output.</li>
<li>Third method is to use set <code>logfile_read</code> to a file. All the output
will be logged to this file. Before the <code>send</code> for which the
<code>expect</code> is used, get the position of file pointer. Now get the
position of file pointer after the send as well. Search for expected
pattern in the file in between first and second pointer.</li>
</ol>

<p>First method is the ideal way it should be done.</p>

python python-2.7 pexpect