1

For a project I am working on I want to automate a series of commands to a separate terminal window so that I can control other programs that use the terminal, or just run general Linux commands.

So far I have tried using subprocess to open a new terminal window using

proc = subprocess.Popen('gnome-terminal')

I have also been able to open programs, for example nano.

proc = subprocess.Popen(['gnome-terminal', '--command=nano'])

But then I can't send more commands to the terminal, meaning I can control it as a user, but I can't automate what it does with python. I have tried using subprocess and pipes to send more commands, but that hasn't worked. I also looked into os.system() but couldn't seem to find a solution.

For a quick example

1) Python opens a new terminal

2) send it 'echo this is a test"

3) send it 'ping -c 10 192.168.x.x >> pingout.txt'

4) in the terminal 'nano pingout.txt'

5) write 'this is a test'

6) then '^X', 'y', and Return to save the edited file

Then the python program would continue on.

  • Possible duplicate: https://stackoverflow.com/questions/19880190/interactive-input-output-using-python (the key is using `subprocess.PIPE` in place of all three standard IO file descriptors) – a small orange May 04 '19 at 22:43
  • when you run `nano` in normal terminal (without using Pyhton) then you also can't do other things in terminal - you can't run ping at the same time. If you want to save in file then do `echo 'this is a test' >> pingout.text`. It is less work. – furas May 04 '19 at 22:43
  • @a small orange Thanks for the link. Its not exactly what I am trying to do, but it looks like it has relevant examples of using subprocess.PIPE that can help me figure it out. @furas Sorry if my example was poor. I don't want to use nano at the same time as something else, but I want to be able to control the terminal, and any program running in it from my python program. – NeverSundae May 04 '19 at 23:50
  • there are more popular commands to edit text in script - `sed`, `awk`, `grep`. But if you use Python then you could get all text to Python and edit it in Python, and save from Python to file. This way you can do more and easier with text. – furas May 05 '19 at 00:08

0 Answers0