I'm working on a script that involves ssh into a server and perform some activities in a tmux session. My script looks something like this (simplified version):
#!/usr/bin/python
import pexpect
child = pexpect.spawn ('ssh johndoe@1.1.1.1 -p 1111')
child.expect ('Password: ')
child.sendline (password)
child.expect ('Welcome to your .*')
child.sendline('tmux')
child.sendline('ls -la')
It works fine until the tmux session. The issue arises when I'm inside the tmux session. My script can't send the next line.
I tried multiple child.expect() values after the tmux such as:
child.expect('\n')
child.expect('$')
and even
sleep(2)
No luck. I still couldn't send the next line "ls -la" inside the tmux session.
What's the best practice to achieve this? Thanks.