I would like to be able to simulate the interactive mode with Python.
For example, if I write
docker exec -it c2 bash, followed by a cd /home, I will be able to see what I am doing and where I am. I tried to do so several times and in a several ways via Python, but the best I can do is just forward a command to a docker container from a parent container - without any feedback on what's actually happening on the screen.
from subprocess import Popen, PIPE
import pty
master,slave = pty.openpty()
print(master)
print(slave)
p = Popen(["docker", "exec", "-i", "c2" ,"/bin/bash"], stdin=PIPE)
p.communicate("cd /home;mkdir 1231321".encode())
This code works, with no output. Also, the script automatically exits the container, without me sending "exit" command, which is not something that I want.
I'm curious, if it's possible to do such task, via a python script?
Thanks.
I found a way to achieve what I wanted to do in the beginning, leaving answer below since it may be helpful to someone.
In order to simulate interactive mode, I used python pexpect library. Following format should do the trick.
p = pexpect.spawn(cmd)
p.expect(/regex/)
p.sendline("what you'd like to send")
Repeat.