Python Pexpect spawn object Flush

本秂侑毒 提交于 2019-12-11 06:01:08

问题


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

The code looks like this:

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 >

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

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

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.

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.

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.

回答1:


Do

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

instead of

child.sendline(cmd)

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



来源:https://stackoverflow.com/questions/19433046/python-pexpect-spawn-object-flush

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!