Python Pexpect生成对象Flush [英] Python Pexpect spawn object Flush

查看:123
本文介绍了Python Pexpect生成对象Flush的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,在其中使用pexpect创建派生对象.

I have a script where i am creating a spawn object using pexpect.

代码如下:

self.rshcmd='rsh 192.X.X.X'
self.pipe1 = pexpect.spawn(command=self.rshcmd, logfile=sys.stdout,maxread=512)

现在,进入该内容后,我希望得到的提示是>

Now after I get into this, I expect for prompt which I get is >

现在我通过发送su然后提供密码来成为su.现在,我的提示如期变成#

And now I become su by sending su and then giving the password. Now my prompt as expected becomes #

我有一个连接到此生成的PC(192.X.X.X)的路由器,它是192.168.1.1

I have a router connected to this spawned PC (192.X.X.X) which is 192.168.1.1

我通过发送telnet 192.168.1.1远程登录到该路由器.完成此路由器的操作后,我想退出telnet会话.

I telnet into this router by sending telnet 192.168.1.1. After I am done from this router, I want to get out of the telnet session.

所以我发送exit并期望#,因为我将回到生成的PC的根目录.但是我看到的是我实际上正在得到<作为提示.

So I send in exit and expect for # as I will get back to the the root of the spawned PC. But what I see is that I am actually getting < as my prompt.

因此,它以某种方式发送了两次退出"消息.一个带我离开telnet,另一个带我离开根.我认为管道没有被冲洗掉,我们得到了一些旧的堆叠命令.

So somehow it is sending "exit" twice . One takes me out from telnet and other one takes me out from being root. I think the pipe is not flushed out and we get he some old stacked commands being sent.

有没有办法清除管道中所有较旧的命令?

Is there a way i can flush out all the older commands in the pipe?

从文档中可以看到一个冲洗函数,但它似乎无能为力:

From the documentation there is a flush function, but it seems to do nothing:

flush(self)
    This does nothing. It is here to support the interface for a
    File-like object.

推荐答案

要做

child.send(cmd)
child.pexpect(cmd)
child.send('\n')

代替

child.sendline(cmd)

这将解决您的问题. (但是在发送密码时,请执行child.sendline(passwrd))

This will solve your problem. (but when sending password do child.sendline(passwrd) )

这篇关于Python Pexpect生成对象Flush的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆