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

Deadly 提交于 2019-11-30 07:37:07

问题


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 a way to overwrite the need for authentication each time it runs? I don't want to have to type my username and password each time this script runs after login. any help? (and in very simple terms to as I'm a novice.) Much Thanks!


回答1:


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!




回答2:


Another solution is editing the

etc/sudoers

configuration file.

A setting on that file can allow a specific user to execute a specific commands (with... yes... specific parameters) as super user.

If the command itself is not the problem, but the problem is exposing the password in the code then this may be the solution.

The sudores file should be edited running the command visudo as super user.

Before you start tampering with sudoers I strongly suggest you to get a basic knowledge of visudo and the sudoers syntax, as messing that file may causes serius issues to the system.

As you know what you are doing is just a matter of adding a couple of lines.

For information you may Google or start here http://www.sudo.ws/sudoers.man.html




回答3:


If you want all Administrator accounts to be able to use the sudo command without entering a password, then do the following.

Change the line shown below in the /private/etc/sudoers file from

%admin  ALL=(ALL) ALL

to

%admin  ALL=(ALL) NOPASSWD: ALL

This edit can be accomplished, by using the Terminal and TextEdit applications. Open the Terminal application and type the following commands:

cd ~/desktop
sudo cp -n /etc/sudoers /etc/sudoers.orignal
sudo cp /etc/sudoers sudoers.txt
sudo chmod ug+w sudoers.txt
open sudoers.txt
visudo -c -f sudoers.txt
sudo cp -X sudoers.txt /etc/sudoers

When done, the sudoers.txt file on your desktop can be put in the trash.

To undo your changes, use the command:

sudo cp /etc/sudoers.original /etc/sudoers

This was tested using OS X 10.10.1

If you want to do the same for a single user then see: http://hints.macworld.com/article.php?story=20021202054815892

Below is a brief explanation of what each command does:

cd ~/desktop

This makes sure you are working from your desktop folder.

sudo cp -n /etc/sudoers /etc/sudoers.original

This backups your sudoers file. The backup can be used to undo your changes. The -n option insures that an existing sudoers.original file will not be overwritten.

sudo cp /etc/sudoers sudoers.txt

Copies the sudoers file to your desktop. The .txt extension is added so OS X will know this is a text file.

sudo chmod ug+w sudoers.txt

Changes the file’s permissions to allow write access.

open sudoers.txt

Opens the file in the TextEdit application. You need to edit the file and save the changes.

visudo -c -f sudoers.txt

Checks the edited file for syntax errors. The output should be sudoers.txt: parsed OK.

sudo cp -X sudoers.txt /etc/sudoers

Copies the file back to the /etc directory.



来源:https://stackoverflow.com/questions/14598849/how-to-overwrite-the-asking-for-authentication-when-running-an-admin-shell-scrip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!