call interactive script with administrator privileges

having a bit of trouble googling for or working out an answer to this.

have a small script that i just want to use to call an interactive shell script (takes user input) with admin privileges.

would like to be able to open this applescript as any admin user, take the password, and then run the relevant shell script with root (sudo) privileges.


	tell application "Terminal"

		-- OPTION 1
		-- This will open the script in terminal and correctly take user feedback but script fails with no admin
		activate
		do script installScript

		-- OPTION 2
		-- This will prompt for password and then hang with no output to terminal or applescript editor 
		activate
		do shell script installScript with administrator privileges

	end tell

i have also tried using chmod 4755 on the script called but it still seems to fail to execute as root when done through above methods or when executed through GUI. sudo ./myscript.sh works as expected though.

Thanks for any and all ideas on correctly invoking my script with sudo/root privileges and input/outputting through terminal.

Hi sarsaeol,

Isn’t there more to do shell script, like with password. I’m thinking that won’t work as root anyway though.

Edited: I gotta cut the grass. Just when it might get interesting.:confused:

gl,
kel

Hello.

You can’t execute a shell script with setuid set.

I don’t think you can make a script execute conventionally in a terminal window with sudo from applescript either.

One way that will work, is having an applescript start a terminal session, run a script that creates a pipe, by the mkpipe command, which you willl then cat from the do script in your terminal session.

You’ll also use that pipe, and send output from a do shell script with administrator privileges through that pipe, that way, your script seems like interactive, showing the result of a script that runs as root.

You’ll have to enter the password for the do shell script command. I see no way around that, retaining a little bit of security on your computer.

Edit

you may have to use the read command from the terminal, instead of cat, in order to detect eof and such. Please look for the post: “Cheapest IPC Ever” in Code exchange, where I explain the details much better, as it is a very early morning/late night here.

I don’t understand why the root? You want administrator privileges.

Can somebody inform me in this.:slight_smile:

Sorry for mixing the terms root is the unix counter part of Administrator Privileges, or stronger, because then you don’t sudo a command, then you have root rights all the time.

Option 1 needs to be given root access as you would type in it normally. So you need to do script “sudo…” then the next line you need to enter the password (can be a result from an display dialog).

Option 2 can’t be executed because the do shell script command isn’t part of the Terminal app. It’s an command that’s comming from the standard scripting addition and should only be executed by the current application, no other target application. The reason for that is that do shell script is using the shell of the current application, also using it’s standard in- and output. Terminal application has it’s standard in- and output already connected to an tty device which results in that you script will wait forever until terminal application re-opens these file descriptors.

Does installScript require an interactive shell? Some scripts demands that bash is launched with option -i that it is interactive. do shell script is not an interactive shell so you have to stick with the terminal then or launch an new interactive shell and run the script in there.

Assuming that the script needs interaction but the shell doesn’t need to be interactive per se. I have an example here how you can work this out with the expect command. Expect is an small scripting language that run your code and when an expression has returned you can respond to that. Here I have an example how you can use it with ssh

adding the preceding sudo does fine for my purposes as the terminal will then prompt for the password, I would have liked a separate dialog box, but it is by no means a requirement. So your suggestion solved my issue!

In any case, thanks to Kel1, McUsrII, and DJ Bazzie Wazzie! I learned some nice new tidbits and am excited to play with ‘mkpipe’ and ‘expect’ in future scripts.

LOVE THIS FORUM! thanks for the top notch community!

<3 <3 <3
sarsaeol