Search code examples
pythonpexpect

Pexpect - Wait to finish a command before continue


I have a current situation with pexpect.

child = pexpect.spawn(f"virsh console {hostname} --force", timeout=200, maxread=4000)

child.sendline("sudo config ztp disable -y")
child.send("\r")
print(" Waiting ZTP ")
sleep(120)
# rest of my script

This command can take 1-2min to finish, now I'm doing a sleep after it "sleep(120)"

I would like to know if there is a way to perform it using pexpect arguments instead of sleep, so I can continue my pexect script as soon as the disable command finishes instead of having a fixed sleep time.


Solution

  • You can try using waitnoecho(), it worked for my case. Here is the link for the docs. Share with me if you found different working solution.