How to overwrite the asking for authentication when running an admin shell script in Apple Script?

前端 未结 3 543
夕颜
夕颜 2020-12-17 05:01

I\'m wanting to make a simple program that runs each time on login behind the UI. In my applescript I\'m running a sudo command that requires admin authentication. Is there

3条回答
  •  悲&欢浪女
    2020-12-17 05:12

    You can put your username and password in the applescript command so that it doesn't ask for those credentials. However note that these items are stored as plain text inside the applescript and thus it's possible for others to see them. It's not really secure but it's up to you to decide if it's safe. NOTE: you don't need "sudo" in the command any longer.

    do shell script "whatever" user name "username" password "password" with administrator privileges
    

    There are methods where you can store your password in the Keychain and retrieve it from the applescript, thus making it secure. If you want to do that then you create the password item as follows.

    Open Keychain Access application and select the keychain in the left column. Then click File>New Password Item..., give it a name, put your account shortname in account, and enter the password. Highlight it in the password list and get information on it. Under the Attributes button enter its kind as generic key. This is chosen because there aren't many of them and the search is much faster. Whatever name you give to it must be put in the code below in "Your Password Name".

    Now from applescript you can use it like this...

    set myPass to getPW()
    
    do shell script "whatever" user name "username" password myPass with administrator privileges
    
    on getPW()
        do shell script "security 2>&1 >/dev/null find-generic-password -gl \"Your Password Name\" | awk '{print $2}'"
        return (text 2 thru -2 of result)
    end getPW
    

    Good luck!

提交回复
热议问题