Search code examples
pythonsubprocesspexpect

Pexpect process dies instead of sending command


So, I'm trying to build a python script that opens Apache Karaf and automatically installs some features. I've tried using Subprocess' Popen but I found it surprisingly complicated. I was suggested to use the pexpect library.

So far I've come up with this:

karaf = "bash /home/karaf_test/bin/karaf"
p = pexpect.spawn(karaf)
p.logfile = p.stdout.buffer
p.expect("root", timeout=10)
p.sendline(feature_install)

Karaf's subshell opens correctly and the feature_install string appears to go through, but the process dies right after without actually executing the command. There's a screenshot for clarity.

I've looked at the documentation but everything seems to be okay, I have no idea why it won't execute. I have tried running send() instead of sendline() to no avail. If anyone has any advice on this I'd be really thankful.


Solution

  • p.expect("root", timeout=10) expect the pattern not in string.so change your code like this

    p.expect(".*@.*()>",timeout=10)