0

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.

ThomasWest
  • 485
  • 1
  • 7
  • 21
  • Tmux is a complex terminal emulator. I'm not at all surprised that it doesn't work well with pexpect. What exactly are you trying to accomplish? Do you need to do it under tmux, or can you just interact with the target application directly? If you just want to remotely run commands on a box, you might consider something like [Fabric](http://www.fabfile.org/) instead. – Jonathon Reinhart Feb 09 '20 at 01:13
  • @JonathonReinhart Thanks for the reply. The only reason why I'm using tmux is because I will eventually run some command that will last for hours to finish the task. So, to answer your question, yeah, I think for now I need that tmux. Is there any better way to achieve this? – ThomasWest Feb 09 '20 at 03:33
  • Using tmux for that reason is still overkill. See perhaps https://stackoverflow.com/questions/8775598/start-a-background-process-with-nohup-using-fabric – Jonathon Reinhart Feb 09 '20 at 04:42

0 Answers0