macos Mac OS X 是否有任何图形“sudo”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1517183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:27:50  来源:igfitidea点击:

Is there any graphical "sudo" for Mac OS X?

macosshellsudoprivilegesgksudo

提问by Johannes

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:

我正在用 Java 设计一个小软件。我不知道我在做什么的术语/定义,但我正在将命令从 Java 提示到终端。像这样的东西:

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

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

我以前在 Linux 中做过这个,我用于gksudo需要 root 密码的命令。

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

gksudoOS X 中有没有?任何图形弹出窗口要求输入 root 密码?

回答by ZJR

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

您可以更多或更少地使用 AppleScript shell 脚本编写自己的脚本:

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

cocoasudolooks aesthetically more pleasing, but this is already deployed.

cocoasudo看起来更美观,但这已经部署了。

回答by tam7t

This also looks promising: cocoasudo

这看起来也很有希望: cocoasudo

enter image description here

在此处输入图片说明

It uses the OSX native Authorization Services API:

它使用 OSX 本机授权服务 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.

对于基于 Mac OS X Cocoa 的应用程序,通过授权服务 API 提供类似的 sudo 功能。使用 API 允许您提示用户输入他们的用户名和密码,请求提升权限的能力。

对于这种情况,我编写了一个名为 cocoasudo 的小实用程序。使用 cocoasudo 的方式与使用 sudo 的方式大致相同。但是,他们不会在终端窗口中提示用户输入密码,而是通过授权服务 API 获得对话框提示。

回答by shannah

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.

我发现如果您正在运行调用其他命令的 shell 脚本,cocoasudo 将不起作用。您还必须在所有子命令中使用 cocoasudo,这也会为每次调用弹出提示。

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

osascript 解决方案似乎工作得更好,但我需要调整它以处理涉及包含空格的路径的命令。

#!/bin/sh
export bar=""
for i in "$@"; do export bar="$bar '${i}'";done
osascript -e "do shell script \"$bar\" with administrator privileges"

回答by ademar111190

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

执行以下操作,在此示例中,我创建一个文件夹 /var/lock 并将您的权限设置为 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.

在 linux 上,也许你可以用 gksudo 来做这个,但我没有测试它,在我去测试并在这里发布结果之后。

回答by LiraNuna

gksudois the GTK+ version of sudo.

gksudo是 GTK+ 版本的sudo.

You can use this clonefor it especially for OS X.

您可以将这个克隆用于它,尤其是在 OS X 上。

回答by cregox

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

按照ZJR 的回答,我已经把它变成了自动机,所以你可以将它用作服务或其他任何东西:

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:

或者,也许你只是认为他的答案已经过时,仍然想要一个 AppleScript,只需在脚本编辑器中写下这一行:

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

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

然后您可以将其制作成应用程序并将其用作服务或其他任何内容。

回答by cregox

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

应该使用本机 OS X 授权服务,而不是查看 sudo 和/或图形界面。

Ref:

参考:

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

[我知道这是一个迟到的答案......]

回答by stippi

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

首先,就像问题的发布者一样,我也有需要从 Java应用程序内部提升权限的情况。

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.

我已将其拆分为两个脚本。第一个脚本是使用一些命令行参数从 Java 执行的。第二个脚本执行所有需要 root 权限的步骤。这个想法当然是在第一个脚本中使用 cocoasudo 以 root 权限执行第二个脚本。

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.

从 Java 应用程序启动时,我确实收到了 cocoasudo 提示,但没有任何反应。甚至没有出现第二个脚本的第一个日志输出。

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.

当我将第一个脚本更改为使用 osascript 时,再次确认就脚本而言一切都是正确的,当它从 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.

这一切都在 OS X Mountain Lion 上。好像 Apple 内置了一些安全防护措施,以防止在 Java 中以 root 权限执行脚本。

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.

由于 cocoasudo 本身实际运行,我倾向于认为解决方案是编写类似于 cocoasudo 的代码,但使用 Cocoa API 调用执行所有其余所需的操作。这也可以是代码签名的。

回答by Adam Batkin

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 gksudowhich I believe uses a graphical popup). Sudo works on both Linux and OS X.

如果您使用的是终端,则只需使用“ sudo”,它会在终端本身提示用户输入密码(gksudo我认为使用图形弹出窗口相反)。Sudo 适用于 Linux 和 OS X。