0

I have an embedded system on which I run code live. Every time I want to run code, I start two scripts in two different terminals: "run1.sh" and "run2.sh". I can see the output of those scripts in my terminals (I wish to too).

Now I want to make a python script that starts those two scripts in two different terminals. I want to still see their output. Also I want to insert a password from the python script to the terminals, since the scripts run in sudo mode. I've played a lot with supbrocess and the PIPES but I've never achieved all of the above requirements simultaneously. How can these requirements be met?

I'm using Ubuntu btw (so I have gnome terminal)

Update : I was probably not clear in my question, but this has to be inside a python script. It is not for my convenience, it's part of an integration process. The code of the script will be part of a larger python program, so the whole point of the question is how do I do it in python.

Tasos
  • 1,575
  • 5
  • 18
  • 44
  • What you mean a "two different terminals"? – xrisk Sep 17 '18 at 17:47
  • You're probably better off using tmux/screen as root in the first place and avoiding python entirely and the question of "how do I grant just the privileges I need without giving them to any graphical processes". – o11c Sep 17 '18 at 17:50
  • @Rishav two different gnome terminals – Tasos Sep 17 '18 at 18:02
  • Very often "I want to run this in two different windows" is a bad design decision, just run the user run them in screen / nohup / whatever. – tripleee Sep 17 '18 at 19:00
  • @tripleee The user in this case has not idea what to do, besides clicking buttons in a UI. That is why I am focusing the question on integration and not friendliness – Tasos Sep 17 '18 at 19:32
  • @Tasos small tip, `sudo` has a `-S` flag that allows reading the password from stdin. – xrisk Sep 20 '18 at 05:50

2 Answers2

2

Based on your new information added I've created an small python script which will launch two terminals and their output separately:

enter image description here

Main script:

mortiz@florida:~/Documents/projects/python/split_python_execution$ cat split_pythonstuff.py 
#!/usr/bin/python3
import subprocess

subprocess.call(['gnome-terminal', '-x', 'python', '/home/mortiz/Documents/projects/python/split_python_execution/script1.py'])
subprocess.call(['gnome-terminal', '-x', 'python', '/home/mortiz/Documents/projects/python/split_python_execution/script2.py'])

Script 1:

mortiz@florida:~/Documents/projects/python/split_python_execution$ cat script1.py 
#!/usr/bin/python3

while True :
   print ('script 1')

Script 2:

mortiz@florida:~/Documents/projects/python/split_python_execution$ cat script2.py 
#!/usr/bin/python3

while True:

    print ('script 2')

From here I guess you can develop anything you want.

UPDATE: About sudo

Sudoers is a great way of controlling which things can be executed by specific users providing passwords or not.

If you add this line in /etc/sudoers there's not need for a password when you pass sudo to your command:

<YOUR_USER> ALL = NOPASSWD : /usr/bin/python <SCRIPT.py>

In your question as far as I understand you have the password stored inside the script. There's no need to do that and it's a bad practice. Sudoers would be a better way.

Anyway, if you want to do it in an insecure way then refer to this question and place it before the commands in the scripts provided in this answer.

The linked provided works:

echo -e "mypassword\n" | sudo -S python test.py
15

You only need to implement that on the previous code.

Miguel Ortiz
  • 1,412
  • 9
  • 21
  • This does indeed work, I've made similar tests alone, but it does not cover the bash + sudo requirements. However you did remind me that I can chain python calls to maybe achieve what I want. – Tasos Sep 17 '18 at 19:33
  • Just pass `sudo run1.sh` instead of `python script.py` – tripleee Sep 17 '18 at 19:36
  • Yea I've already tried that, but it does not yet cover automatically entering the password. – Tasos Sep 17 '18 at 19:37
  • Updating the sudoers file requires access to the computer (so not an option, we need a python solution). I've already checked the question you linked, but I've tried it along with subprocess/os.popen and it does not work. I need a python solution that meets all the requirements stated in the question. Since this seems really hard to implement, I am now investigating into removing sudo privilege requirements from my execution chain completely. – Tasos Sep 18 '18 at 14:45
  • @Tasos the link does work, I've tested it and put the output in the answer. You only need to append that code inside the python subprocess. Stackoverflow is a place where you should show your work and people help you to complete it, not the other way. At this point the answer provides the tools for your needs. – Miguel Ortiz Sep 18 '18 at 15:40
0

You could install Terminator and configure one profile per terminal to run any script you want.

I have a default template which will load 3 terminals and run 3 different commands / or scripts if you wanted to:

enter image description here

When I load that profile the first one will move me to my projects dir and list them. The next one will run df -h to see the space available and the lower my ip configuration.

This way would save you lots of programming and it's quite easy.

UPDATE: It will run any command, bash, zsh, python, etc.. available for your terminal. If the script is locally in your machine:

python <your_script_1> # first terminal profile
python <your_script_2> # second terminal profile

both would be executed "at the same time".

If your scripts are remote in the target machine, simply create a bash script using ssh to connect to the remote machine with a private key and then running the script, the result is the same in both scenarios.

EDIT: The best thing is setting colors and transparency for each terminal, so you can enjoy the penguin's selfie while you work.

Miguel Ortiz
  • 1,412
  • 9
  • 21
  • Sorry, I was probably not clear in my question. It has to be in a python script, it's going to be integrated with other python code. I've updated the question to make it clear. – Tasos Sep 17 '18 at 18:10
  • @Tasos You can run any command you want. It could be python or bash. – Miguel Ortiz Sep 17 '18 at 18:11
  • Yes, but then I will need to install & configure terminator in every machine that the code is going to be run on. It is many machines that I don't even have access to, so it's not an option – Tasos Sep 17 '18 at 18:15
  • @Tasos Not true, you only need to install terminator in your machine and execute the code remotely in the rest of your machines. – Miguel Ortiz Sep 17 '18 at 18:18
  • As I said, I don't have access to any machine – Tasos Sep 17 '18 at 18:29
  • @Tasos got it, I've added a new answer using python3. – Miguel Ortiz Sep 17 '18 at 18:57