是否有任何图形化的“sudo”?适用于 Mac OS X?

发布于 2024-08-06 04:06:38 字数 285 浏览 6 评论 0原文

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

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

我之前在 Linux 中做过这样的事情,并且我使用 gksudo 来执行需要 root 密码的命令。

OS X 中有 gksudo 吗?是否有图形弹出窗口询问 root 密码?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

绅刃 2024-08-13 04:06:38

您可以更轻松地使用 AppleScript shell 脚本编写自己的脚本:

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

cocoasudo 看起来很美观更令人高兴,但这已经部署了。

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.

请你别敷衍 2024-08-13 04:06:38

这看起来也很有希望: < code>cocoasudo

在此处输入图像描述

它使用 OSX 本机授权服务 API:

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

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

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.

不再让梦枯萎 2024-08-13 04:06:38

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

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

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

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"
鹿童谣 2024-08-13 04:06:38

进行以下操作,在这个示例中,我创建一个文件夹 /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();
}

在 Linux 上,也许您可​​以使用 gksudo 进行此操作,但我没有测试它,在我进行测试并将结果发布到此处之后。

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.

浅浅 2024-08-13 04:06:38

按照ZJR的回答,我已将其制作成自动化程序,因此您可以将其用作服务或其他什么:

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

    return input
end run

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

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

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

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.

纵山崖 2024-08-13 04:06:38

gksudosudo 的 GTK+ 版本。

您可以使用此克隆,特别是对于 OS X。

gksudo is the GTK+ version of sudo.

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

桃气十足 2024-08-13 04:06:38

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

参考:

[我知道它是迟到的答复...]

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

潜移默化 2024-08-13 04:06:38

这些答案中似乎有很多错误的信息。为了节省其他人的时间,我发布了自己的发现:

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

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

我通过大量记录到单独的文件中确认这些脚本确实达到了我的预期。当从命令行手动启动(当然具有普通用户权限)时,它们可以正常工作。

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

当我将第一个脚本更改为使用 osascript 时,再次确认脚本中的所有内容都是正确的,当它从 Java 内部运行时,我什至没有收到提示。

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

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

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.

安稳善良 2024-08-13 04:06:38

如果您使用的是终端,则只需使用“sudo”即可,这将在终端本身中提示输入用户密码(而不是 gksudo,我相信它使用的是图形弹出窗口)。 Sudo 可在 Linux 和 OS X 上运行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文