简体   繁体   English

Pexpect:从最后一次发送中读取

[英]Pexpect: Read from the last send

I am trying to read the output of pexpect.send(cmd) but here's the problem I am facing. 我正在尝试读取pexpect.send(cmd)的输出,但这是我面临的问题。

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 expect matches from the point it last read. 但是,从最后一次阅读的角度来看,应该expect匹配。 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. 我曾尝试过其他方法,例如在发送需要输出的命令之前匹配EOF,但EOF表示孩子已终止。 I have tried reading till timeout and then sending the command but timeout itself causes the child to terminate. 我尝试读取直到超时,然后发送命令,但是超时本身导致孩子终止。

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. 似乎没有可靠的方法来执行此操作。

Could anyone help me sort this out ? 谁能帮我解决这个问题?

There are three ways in which this problem can be handled but none to flush the buffer 有三种方法可以解决此问题,但没有一种方法可以清除缓冲区

  1. In Pexpect every send call should be matched with a call to expect . 在Pexpect中,每个send呼叫都应与expect呼叫匹配。 This ensures that the file pointer has moved ahead of the previous send. 这样可以确保文件指针已移到上一个发送之前。
  2. If there is a series of send before a single expect then we need to provide a way to move file pointer to the location of last send. 如果在一个expect之前有一系列send ,那么我们需要提供一种将文件指针移动到最后发送位置的方法。 This can be done by an extra send whose expect output is unique. 这可以通过额外send来完成,其expect输出是唯一的。 The uniqueness should be such that none of the send in the series of send should give that output. 唯一性应确保send系列中的任何send都不应该提供该输出。
  3. Third method is to use set logfile_read to a file. 第三种方法是使用set logfile_read到文件。 All the output will be logged to this file. 所有输出将记录到该文件。 Before the send for which the expect is used, get the position of file pointer. 在使用expectsend之前,获取文件指针的位置。 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. 在第一和第二指针之间的文件中搜索期望的模式。

First method is the ideal way it should be done. 第一种方法是应该完成的理想方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM