Is there any graphical “sudo” for Mac OS X?

2019-01-22 07:22发布

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?

9条回答
迷人小祖宗
2楼-- · 2019-01-22 07:55

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.

查看更多
Root(大扎)
3楼-- · 2019-01-22 07:58

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"
查看更多
Explosion°爆炸
4楼-- · 2019-01-22 07:59

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.

查看更多
时光不老,我们不散
5楼-- · 2019-01-22 08:04

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.

查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-22 08:09

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.

查看更多
7楼-- · 2019-01-22 08:09

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 ...]

查看更多
登录 后发表回答