Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Do Shell Script .. with Administrator Privileges ... ?

hi everyone!

I have a script that have multiple lines like this:

do shell script " ... " with administrator privileges
do shell script " ... " with administrator privileges

Mac OS X 10.4 does it right(?) by asking ONCE for an admin login/password, and would run both lines above ... but 10.2 (and 10.3?) asks TWICE for the password using the same code. What am I doing wrong? I want all versions 10.2-4 to ask only once ... Any ideas?

Thank you!!
Cass 🙂

Mac OS X (10.4.5)

Posted on Mar 21, 2006 9:13 PM

Reply
6 replies

Mar 21, 2006 9:35 PM in response to cassie21ts

Try this:

display dialog "Your password is required." with icon note buttons {"Cancel", "OK"} default button "OK" default answer ""
set the_pass to the text returned of the result
do shell script "ls -l /" password the_pass with administrator privileges
do shell script "ls -l /" password the_pass with administrator privileges
do shell script "sudo -k"
--expires the timestamp set by previous commands so nobody can perform additional ones without the password. This is automatic in 10.4, and the line has no effect
set the_pass to {random number, random number, random number}
--prevents password from being kept in memory longer than needed

Your password will be shown in the clear while being typed.

(11246)

Mar 21, 2006 10:00 PM in response to Niel

If you would prefer that your script would not ask for a password to get admin privileges, then this uses the keychain:

set aVariable to do shell script DefinedAsVariable password getPassw() with administrator privileges

-- The Handler --
(*
The following handler assumes you have a password in your keychain called ASPW (could be anything you want) saved as a generic key. To do that, open your Keychain Access (in Utilities) application, choose file New, and in the sheet give it a name, enter your account name and type the password you want to use (your admin password in this case). OK. Now find the new password and double-click it. Under the attributes tab change Kind to generic key (it will be Applications). This makes for a fast search because there aren't many of them. Switch to the access control tab and select the "Allow all applications to access this item" button. Enter your admin password in the dialog that appears, and you are done. The first time you run the script, you'll have to click "Always Allow" in the dialog that appears - perhaps several times. After that, it won't ask.
*)

to getPassw()
tell application "Keychain Scripting"
launch
tell current keychain to ¬
tell (some generic key whose name is "ASPW")
return password
end tell
end tell
end getPassw

Mar 21, 2006 10:07 PM in response to cassie21ts

The other approach is to string the shell commands together into one do shell script command:

do shell script "command1; command2; command3" with administrator privileges


BTW, this isn't anything you're doing wrong, it's something that Apple changed in later version of Mac OS X to manage the authentication process differently, partly to get around the issue of one script prompting multiple times.

Mar 28, 2006 9:02 AM in response to cassie21ts

The method of stringing all the commands together into one do shell script command will work in all versions of Mac OS X.

On pre-10.4 systems it's a good idea to include a 'sudo -k' as the last command to clear the authentication cache, but it isn't absolutely necessary, and also won't cause a problem if you do include it under 10.4.

The only other thing to be aware of is timeouts.
Since it all runs as a single AppleScript command AppleScript will apply its standard timeout (30 seconds? 60 seconds? I forget which) to the entire list of commands, as opposed to using that timeout for each command. If any individual command takes a long time it's possible that the overall time exceeds that timeout, whereas running the commands individually does not. The solution here is to wrap the command in a with timeout... block.

Mar 28, 2006 8:50 PM in response to Camelot

The standard timeout for an application's tell block is 60 seconds; this limit does not appear to apply to do shell script. I ran the following:

with timeout of 3 seconds
do shell script "top -l 5"
end timeout

and didn't get a timeout error. The command took 5 seconds to complete. On the other hand, if you are using the do shell script line inside a tell block, you will get a timeout error if the command takes too long.

(11384)

Do Shell Script .. with Administrator Privileges ... ?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.