display
display

Reputation: 51

Inputting password for sudo command via python script

Bit of a strange one. I have a python file that is activated by an AppleScript. My python script does a couple of basic commands. One thing it does is try and run a sudo command in a terminal window. My problem is that a sudo command needs a password to be executed. I can't figure out how to enter my password in the python file, as the code wants a password input for in order to execute the sudo command.

The apple script below calls a python script known as setting_pm.py to be run from my desktop folder.

Apple script:

set desktop_folder to "$HOME/Desktop"
tell application "Terminal"
    activate
    do script "cd desktop;cd setting_pm;python3 setting_pm.py"
end tell

Python file:

os.system("sudo pmset schedule cancelall")
os.system("myPassword")

If you test the code above you will see that the password gets asked for but it wont seem to accept the next command where i try and input my password.

I have played around with removing passwords from sudo but i like the idea of having passwords enabled, and only entering the password in this specific situation from a python script.

Upvotes: 2

Views: 1104

Answers (1)

display
display

Reputation: 51

answer:

import os
os.system("echo password | sudo -S pmset schedule cancelall")

Upvotes: 3

Related Questions