26

I'm designing a little software in Java. I don't know the term/definition to what I'm doing, but I'm prompting commands from Java to the terminal. Something like this:

Process process = Runtime.getRuntime().exec("command");

I've done this before in Linux, and I used gksudo for commands that required the root password.

Is there any gksudo in OS X? Any graphical popup asking for root password?

Shedokan
  • 1,212
  • 1
  • 10
  • 21
  • 1
    when I open mamp, I'm asked to give my password. Or when I install a pkg.. what is that pop-up box called in os x? –  Oct 05 '09 at 21:29

9 Answers9

25

You can more ore less manage to write your own with an AppleScript shell script:

#!/bin/sh
osascript -e "do shell script \"$*\" with administrator privileges"

cocoasudo looks aesthetically more pleasing, but this is already deployed.

ZJR
  • 9,308
  • 5
  • 31
  • 38
  • 2
    Any way to remember the entered password between calls? I need to make several calls to such a script in succession, and don't want require the user to enter their password multiple times. – Kevin Wong Oct 23 '13 at 14:41
  • 1
    This isn't working anymore. Check my answer for updates or more options. Feel free to update yours, @ZJR! ;P – cregox Nov 19 '15 at 14:09
7

This also looks promising: cocoasudo

enter image description here

It uses the OSX native Authorization Services API:

For Mac OS X Cocoa-based apps, there is analagous ability to sudo provided via the Authorization Services API. Use of the API allows you to prompt the user for their username and password requesting the ability to escalate privileges.

For that case, I’ve written a small utility that I’ve dubbed cocoasudo. Use cocoasudo in much the same way you’d use sudo. However, instead of users being prompted for their password in a Terminal window, they’ll get a dialog prompt via the Authorization Services API.

Community
  • 1
  • 1
tam7t
  • 111
  • 1
  • 4
  • Somehow this fails to work for me: I tried `./cocoasudo systemsetup` and got the graphical prompt, however after entering my password, `systemsetup` still think I'm not `sudo`ing as it complains: `You need administrator access to run this tool... exiting!` – h__ Oct 24 '14 at 10:26
6

make the follows, in this example I go create a folder /var/lock and set your permissions to 777:

String[] command = {
        "osascript",
        "-e",
        "do shell script \"mkdir -p /var/lock && chmod 777 /var/lock\" with administrator privileges" };
Runtime runtime = Runtime.getRuntime();
try {
    Process process = runtime.exec(command);
    BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
    String line;
    while ((line = bufferedReader.readLine()) != null)
            System.out.println(line);
} catch (IOException e) {
    e.printStackTrace();
}

on linux maybe you can make this with gksudo but I not test it, after I go testing and post here the results.

ademar111190
  • 14,215
  • 14
  • 85
  • 114
6

I found the cocoasudo doesn't work if you are running a shell script that calls other commands. You would have to use cocoasudo in all sub-commands also which would pop up a prompt for each call.

The osascript solution seems to work better, but I needed to tweak it to work with commands involving paths containing spaces.

#!/bin/sh
export bar=""
for i in "$@"; do export bar="$bar '${i}'";done
osascript -e "do shell script \"$bar\" with administrator privileges"
shannah
  • 61
  • 1
  • 2
  • 2
    Awesome. The only drawback is that the dialog reads "osascript wants to make changes", which may confuse the user since she may not know what osascript is. – gregschlom Nov 10 '11 at 15:54
3

Following ZJR's answer, I've made this into automator, so you can use it as a Service or whatever:

on run {input, parameters}
    do shell script "sudo open \"" & (POSIX path of input as string) & "\"" with administrator privileges

    return input
end run

Or, maybe you just think his answer is outdated and still want an AppleScript, just write this single line in Script Editor:

do shell script "[[your command]]" with administrator privileges

Which you can then make into an app and use it as a Service or whatever.

Community
  • 1
  • 1
cregox
  • 17,674
  • 15
  • 85
  • 116
2

gksudo is the GTK+ version of sudo.

You can use this clone for it especially for OS X.

LiraNuna
  • 64,916
  • 15
  • 117
  • 140
1

One should use the native OS X authorization services instead of looking at sudo and/or a graphical interface to it.

Ref:

[I know it's a late answer ...]

0

There seems to be a lot of wrong information in these answers. To save other people some time, I post my own findings:

First of all, like the poster of the question, I also have the situation that I need to elevate permissions from within a Java application.

I have split this up into two scripts. The first script is executed from Java with some command line parameter. The second script performs all steps that need root privileges. The idea is of course to use cocoasudo in the first script to perform the second script with root privileges.

I have confirmed via extensive logging into separate files that the scripts indeed do what I intended. And they work fine when launched manually (with normal user privileges of course) from the command line.

When launched from the Java app, I do get the cocoasudo prompt, but nothing happens. Not even the first logging output from the second script appears.

When I change the first script to use osascript, again with confirmation that everything is correct as far as the script goes, I don't even get a prompt when it runs from within Java.

This is all on OS X Mountain Lion. As if Apple build in some safe guards that prevent scripts being executed with root privileges from within Java.

Since cocoasudo itself actually runs, I am inclined to think the solution is to code something similar to cocoasudo, but performing all the rest of the required actions using Cocoa API calls. This can then be code-signed as well.

stippi
  • 855
  • 8
  • 15
-5

If you are using a terminal, then just use "sudo" instead, which will prompt for the user's password in the terminal itself (as opposed to gksudo which I believe uses a graphical popup). Sudo works on both Linux and OS X.

Adam Batkin
  • 51,711
  • 9
  • 123
  • 115
  • 1
    it's supposed to be graphical ... =) –  Oct 04 '09 at 19:54
  • 1
    What clown downvoted this? When I answered the question, the word "graphical" appeared nowhere in the question. And it's still a valid answer, even if it's not *exactly* what the original poster asked – Adam Batkin Jun 09 '13 at 04:11
  • 1
    The question clearly asks for a graphical version of sudo (word number 4). – 64bit_twitchyliquid Aug 05 '17 at 13:05
  • @64bit_twitchyliquid But it didn't when I answered the question. Plus my answer isn't wrong, it's just not fully answering the question (i.e. it might still help someone else) – Adam Batkin Aug 08 '17 at 05:02