1

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.
Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
user2891472
  • 31
  • 1
  • 5
  • pexpect does not send the commands twice. Could the telnet connection be closing itself before you send `exit`? – Thomas K Oct 28 '13 at 00:49

1 Answers1

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) )

Sagar Shah
  • 123
  • 1
  • 5