3

I have a Mac and I'm programming a sort of security script (I know it's useless and overkill but I'm just trying to expand my knowledge of Python) and one of the steps in my script requires a password from the admin before it can execute.

I want to make it so this script will execute under certain circumstances and I don't want it to require any human interaction. I've tried doing the following:

os.system("sudo -s")            
os.system("my password")

However, it tells me that I typed the password wrong (clearly Apple made it so you can't have a program type it).

In short, I've tried everything. I then figured out that you can copy and paste the password and it works. So my question is: how can I have my script copy and paste the password and then hit enter? Alternatively, is there a better way to achieve this?

Kara
  • 6,115
  • 16
  • 50
  • 57
David
  • 33
  • 4
  • Possible duplicate of [Using sudo with Python script](http://stackoverflow.com/questions/13045593/using-sudo-with-python-script) – Kara Jan 05 '17 at 22:16

1 Answers1

2

I think you need to have a capital "-S", so it should look like:

p = os.system('echo {}|sudo -S {}'.format('my password', 'command to run'))

By the way, the reason for you not to do this is less about how you can mess up your computer using sudo and more about how you are going to have a script on your computer with your sudo password stored in plain text.

Michael Volo
  • 314
  • 3
  • 13