如何在 Xcode 4 中的运行脚本构建阶段使用 sudo?

发布于 2024-11-26 09:51:48 字数 150 浏览 1 评论 0原文

我需要使用 sudo 在 Xcode 4 的运行脚本构建阶段中的脚本内执行命令。然而,编译器抱怨:

sudo:不存在 tty 且未指定 Askpass 程序

有人对这个问题有一个聪明的解决方案吗?

I need use execute a command inside of a script in a Run Script build phase in Xcode 4 using sudo. However, the compiler complains:

sudo: no tty present and no askpass program specified

Anyone have a clever solution for this problem?

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

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

发布评论

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

评论(7

作妖 2024-12-03 09:51:48

一种解决方案是将 sudo 密码放置在可执行 shell 脚本中,如下所示:

#!/bin/bash
echo thesudopassword

该 shell 脚本可能称为password.sh

然后,设置环境变量 SUDO_ASKPASS=password.sh

设置完成后,可以将 -A 选项传递给须藤。此选项使用 ASKPASS 程序来获取 sudo 密码。 ASKPASS 程序只需将密码写入 stdout。

例如,

sudo -A ditto -V /tmp/testserver.dst /

这显然是一个相当不安全的解决方案,但它确实有效。

One solution is to place the sudo password in an executable shell script like the following:

#!/bin/bash
echo thesudopassword

This shell script might be called password.sh

Then, setup the environment variable SUDO_ASKPASS=password.sh

Once this is setup, the -A option can be passed to sudo. This option uses the ASKPASS program to obtain the sudo password. The ASKPASS program need only write the password to stdout.

So, for example,

sudo -A ditto -V /tmp/testserver.dst /

This is obviously a rather insecure solution, but it does work.

咆哮 2024-12-03 09:51:48

尚未提出的两个想法,这两个想法可能都比当前接受的答案更好/更安全:

第一个选项是将需要作为 root 运行的脚本部分放在脚本文件中(.sh 或某事),然后将其设置为 root:chmod go-w,+sx scriptfilesudo chown root scriptfile。这意味着脚本将自动以 root 身份运行,从而避免您需要进行身份验证才能运行它(只是为了更改它)。只要其操作不受用户输入的影响,这应该是相当安全的。 (当然,如果您制作一个脚本,该脚本接受输入参数并删除它或运行它,或者用它执行大多数其他操作,那么这是不安全的。)

第二个选择是使用 applescript (可能通过 osascript)。 Applescript 允许您使用管理员权限执行 shell 脚本“sudo 命令在此处”,这将弹出一个图形对话框,要求输入密码。

第一个选项适用于自动化环境,但它可能无法很好地处理(例如)签入 SCM 或发送给其他用户的情况。第二个选项可以更好地工作,但每次都需要输入密码,因此对于自动构建脚本来说效果不佳。

Two ideas that haven't been suggested yet, both of which are probably better/safer than the currently accepted answer:

First option would be to put the part of the script that needs to be run as root in a script file (.sh or something), and then make it setuid as root: chmod go-w,+sx scriptfile, sudo chown root scriptfile. This means that script will automatically run as root, which avoids you needing to authenticate to run it (just to change it). As long as its operation isn't subject to user input, this should be quite safe. (Of course, if you make a script that takes an input argument and deletes it or runs it, or does most anything else with it, that would not be safe.)

Second option would be to use applescript (possibly via osascript). Applescript allows you to do shell script "sudo command goes here" with administrator privileges, which will pop up a graphical dialog asking for a password.

The first of these options would be good for an automated environment, though it might not deal well with (for example) being checked into an SCM, or being sent to another user. The second option would work better with that, but requires a password input every time, so doesn't work as well for an automated build script.

我爱人 2024-12-03 09:51:48

此问题的另一个解决方案是修改 sudoers 文件并将您的帐户添加到其中,并声明永远不会要求您提供 sudo 密码。要完成此操作相当简单:

运行:

sudo visudo

在用户权限规范部分中添加一行,如下所示

youraccountname ALL=(ALL) NOPASSWD: ALL

当然,这可能是一件危险的事情,所以要小心。我建议在走这条路线之前阅读 sudoers 和 visudo 的手册页。

Another solution to this problem is to modify sudoers file and add your account to it and state that you should never be asked for the sudo password. To accomplish this is fairly straightforward:

run:

sudo visudo

In the User privilege specification section add a line that looks like

youraccountname ALL=(ALL) NOPASSWD: ALL

Of course, this can be a dangerous thing to do, so be careful. I would suggest reading the man page for sudoers and visudo before going this route.

同展鸳鸯锦 2024-12-03 09:51:48

经过大量搜索,我找到了以下解决方案。

https: //forum.juce.com/t/build-script-for-automatically-moving-built-aus-into-components-folder-xcode/13112

摘要

  1. 创建钥匙串并将您的管理员密码存储在钥匙串中
  2. 创建一个使用 /usr/bin/security 来访问密码的脚本在您的运行脚本中,
  3. 设置 ASK_PASS 环境变量并在 sudo 中使用 -A 选项

After much searching I found the following solution.

https://forum.juce.com/t/build-script-for-automatically-moving-built-aus-into-components-folder-xcode/13112

Summary

  1. Create a keychain and store your admin password in the keychain
  2. Create a script which uses /usr/bin/security to access the password In your run script,
  3. Set the ASK_PASS env variable and use the -A option with sudo
灼痛 2024-12-03 09:51:48

您可以使用以下命令直接以管理员身份运行命令(将 echo YourCommandHere > /tmp/hello 更改为您的命令):

osascript -e 'do shell script "sudo echo YourCommandHere > /tmp/hello " with administrator privileges'

或者使用以下命令在源目录中运行脚本:

osascript -e 'do shell script "bash -x $SOURCE_ROOT/MyAdminScript.sh 1>/tmp/build-log 2>/tmp/build-log.err" with administrator privileges'

这会运行脚本并记录日志它输出到 /tmp/build-log 和 /tmp/build-log.err

有关脚本中有用的变量,请参阅 https://help.apple.com/xcode/mac/8.0/#/itcaec37c2a6

You can either run commands directly as a administrator with the following (changing echo YourCommandHere > /tmp/hello to your command):

osascript -e 'do shell script "sudo echo YourCommandHere > /tmp/hello " with administrator privileges'

Or run a script in your source directory using:

osascript -e 'do shell script "bash -x $SOURCE_ROOT/MyAdminScript.sh 1>/tmp/build-log 2>/tmp/build-log.err" with administrator privileges'

This runs the script and logs it output to /tmp/build-log and /tmp/build-log.err

For useful variables in the script see https://help.apple.com/xcode/mac/8.0/#/itcaec37c2a6

哥,最终变帅啦 2024-12-03 09:51:48

您还可以使用 sudo 执行 XCode,将项目作为终端的参数,如下所示:

sudo /Developer/Applications/Xcode.app/Contents/MacOS/Xcode /path/to/your/project.xcodeproj

这是我能想到的最简单的解决方案,但可能存在一些缺点,因为您将以 root 身份执行 XCode。

You can also execute XCode giving it the project as parameter from the Terminal using sudo like this:

sudo /Developer/Applications/Xcode.app/Contents/MacOS/Xcode /path/to/your/project.xcodeproj

This is the easiest solution I could think of, but there may be some drawbacks, since you would be executing XCode as root.

℉服软 2024-12-03 09:51:48

无需在任何地方写下 sudo 密码。只需打开一个终端窗口并输入

$ sudo echo "hello"

一旦你输入了密码,它会在一段时间内有效 - 不确定多久 - 并且 Xcode 生成的 shell 将继承此权限。

如果稍后再次收到“no tty present”消息,只需重复该过程

No need to write your sudo password anywhere. Just open a terminal window and type

$ sudo echo "hello"

Once you've typed your password, it will be good for a while - not sure how long - and the shell spawned by Xcode will inherit this permission.

If you get the "no tty present" message again later, just repeat the procedure

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