I am trying to make a simple Python script that enters a given password in the command line after using the 'su' command (or any other command which requires administrator privileges or simply requires a password in order to be executed).
I tried to use the Subprocess module for this as well as pynput, but haven't been able to figure it out.
import subprocess
import os
# os.system('su') # Tried using this too
process = subprocess.Popen('su', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
process.stdin.write(b"password_to_enter")
print(process.communicate()[0])
process.stdin.close()
I was expecting this to enter 'password_to_enter' in the given password prompt after typing the 'su' command, but it didn't. I tried giving it the correct password as well but still did not work.
What am I doing wrong?
PS: I am on Mac