Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make my script automatically type my admin password into Mac OS terminal?

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?

like image 326
David Avatar asked Oct 21 '25 17:10

David


1 Answers

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.

like image 196
Michael Volo Avatar answered Oct 24 '25 08:10

Michael Volo