是否有任何图形化的“sudo”?适用于 Mac OS X?
我正在用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以更轻松地使用 AppleScript shell 脚本编写自己的脚本:
cocoasudo
看起来很美观更令人高兴,但这已经部署了。You can more ore less manage to write your own with an AppleScript shell script:
cocoasudo
looks aesthetically more pleasing, but this is already deployed.这看起来也很有希望: < code>cocoasudo
它使用 OSX 本机授权服务 API:
This also looks promising:
cocoasudo
It uses the OSX native Authorization Services API:
我发现如果您正在运行调用其他命令的 shell 脚本,则 cocoasudo 不起作用。您还必须在所有子命令中使用 cocoasudo,这将为每个调用弹出提示。
osascript 解决方案似乎工作得更好,但我需要调整它以处理涉及包含空格的路径的命令。
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.
进行以下操作,在这个示例中,我创建一个文件夹 /var/lock 并将您的权限设置为 777:
在 Linux 上,也许您可以使用 gksudo 进行此操作,但我没有测试它,在我进行测试并将结果发布到此处之后。
make the follows, in this example I go create a folder /var/lock and set your permissions to 777:
on linux maybe you can make this with gksudo but I not test it, after I go testing and post here the results.
按照ZJR的回答,我已将其制作成自动化程序,因此您可以将其用作服务或其他什么:
或者,也许你只是认为他的答案已经过时并且仍然想要一个AppleScript,只需在脚本编辑器中编写这一行:
然后你就可以将其制作成应用程序 并将其用作服务或其他内容。
Following ZJR's answer, I've made this into automator, so you can use it as a Service or whatever:
Or, maybe you just think his answer is outdated and still want an AppleScript, just write this single line in Script Editor:
Which you can then make into an app and use it as a Service or whatever.
gksudo
是sudo
的 GTK+ 版本。您可以使用此克隆,特别是对于 OS X。
gksudo
is the GTK+ version ofsudo
.You can use this clone for it especially for OS X.
人们应该使用本机 OS X 授权服务,而不是查看 sudo 和/或它的图形界面。
参考:
授权服务编程指南简介(苹果)
授权服务任务 (apple)
[我知道它是迟到的答复...]
One should use the native OS X authorization services instead of looking at sudo and/or a graphical interface to it.
Ref:
Introduction to Authorization Services Programming Guide (apple)
Authorization Services Tasks (apple)
[I know it's a late answer ...]
这些答案中似乎有很多错误的信息。为了节省其他人的时间,我发布了自己的发现:
首先,就像问题的发布者一样,我也遇到了需要从 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.
如果您使用的是终端,则只需使用“
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 togksudo
which I believe uses a graphical popup). Sudo works on both Linux and OS X.