Saltar al contenido

~ / archivo / stackoverflow / 52373206

Start two shell sudo scripts in two different terminals from python3

StackOverflow · python, shell, terminal, sudo · ayudé
  1. Tasos pregunta 2018-09-17 17:45

    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.

    • xrisk 2018-09-17 17:47 What you mean a "two different terminals"?
    • o11c 2018-09-17 17:50 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".
    • Tasos 2018-09-17 18:02 @Rishav two different gnome terminals
    • tripleee 2018-09-17 19:00 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.
    • Tasos 2018-09-17 19:32 @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
    • xrisk 2018-09-20 05:50 @Tasos small tip, sudo has a -S flag that allows reading the password from stdin.
  2. Miguel Ortiz yo respuesta +2 2018-09-17 18:43

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

    [imagen]

    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.

    • Tasos 2018-09-17 19:33 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.
    • tripleee 2018-09-17 19:36 Just pass sudo run1.sh instead of python script.py
    • Tasos 2018-09-17 19:37 Yea I've already tried that, but it does not yet cover automatically entering the password.
    • Tasos 2018-09-18 14:45 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.
    • Miguel Ortiz yo 2018-09-18 15:40 @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.
  3. Miguel Ortiz yo respuesta 2018-09-17 17:58

    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:

    [imagen]

    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.

    • Tasos 2018-09-17 18:10 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.
    • Miguel Ortiz yo 2018-09-17 18:11 @Tasos You can run any command you want. It could be python or bash.
    • Tasos 2018-09-17 18:15 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
    • Miguel Ortiz yo 2018-09-17 18:18 @Tasos Not true, you only need to install terminator in your machine and execute the code remotely in the rest of your machines.
    • Tasos 2018-09-17 18:29 As I said, I don't have access to any machine
    • Miguel Ortiz yo 2018-09-17 18:57 @Tasos got it, I've added a new answer using python3.

← todos los hilos de StackOverflow