2

I need to create a Python script to open a terminal window in Mac/Linux and need to execute the following commands in terminal from Python script.

  1. Open terminal
  2. Execute the command sudo openpyn --init""
  3. It will prompt for us to enter password and user name - Pass it from Python script

Tried with below but it is not working,Any idea or recommendations please.Will be of great help.

from openpyn import openpyn
import subprocess
import os

subprocess.call(["sudo openpyn --init"], shell=True)
os.system("sudo openpyn --init")

But it is not giving any results or not working as expected.

7
  • Beware, by default sudo reads a password from /dev/tty because a password has no reason to come from another program... Commented Jan 29, 2020 at 12:16
  • 1
    You didn't start a Terminal, you started a shell. Maybe os.system("""osascript -e 'tell application "Terminal" to do script "echo hello"'""") Commented Jan 29, 2020 at 12:19
  • @MarkSetchell Thanks a lot !! os.system("""osascript -e 'tell application "Terminal" to do script "echo hello"'""") added this line terminal window is popping up, But when i try to execute the terminal commands like os.sysstem("""ls -l""") no results are coming any idea how we can do this? Commented Jan 29, 2020 at 12:37
  • @SergeBallesta I was thinking if we can spawn a terminal window and execute the commands from PYTHON ,will it be possible to pass credentials like user name and password from there? Commented Jan 29, 2020 at 12:39
  • 1
    You've done exactly the same thing again. You haven't started a Terminal, you just are running ls without a Terminal. Commented Jan 29, 2020 at 12:39

1 Answer 1

5

I don't fully understand the wording of your question, but if you want to pop up a Terminal and run:

ls -l 

in that Terminal, you can do:

import os
os.system("""osascript -e 'tell application "Terminal" to do script "ls -l"'""")
Sign up to request clarification or add additional context in comments.

3 Comments

This is specific to Mac. If you need something similar on Linux, try xterm -e 'ls -l' & or whichever terminal application you want to spawn.
@MarkSetchall One thing if you can answer this it would be great, What if i want to execute multiple terminal commands in the same terminal window?
Change ls -l to date; ls -l; pwd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.