如何让Automator运行需要sudo密码的Shell脚本?

时间:2017-05-02 22:16:24

标签: bash macos shell terminal automator

我使用下面的脚本在每次运行时随机更改mac地址。

openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//' | xargs sudo ifconfig en0 ether

ifconfig en0 | grep ether

我希望Automator为我做这件事。当我运行这个Shell脚本时,它运行成功,但是当我实际打开终端并运行

ifconfig en0 | grep ether

看看它是否改变了MAC地址我发现它没有。

如果我手动将这样的脚本输入终端,它可以完美地工作。 我该怎么办?

2 个答案:

答案 0 :(得分:2)

尝试改为创建AppleScript

on run {input, parameters}
    do shell script "openssl rand -hex 6 | sed 's/\\(..\\)/\\1:/g; s/.$//' | xargs sudo ifconfig en0 ether" with administrator privileges
    do shell script "ifconfig en0 | grep ether"
    return input
end run

应该要求您输入管理员密码,然后更改mac地址。 Automater通常更适合运行Applescripts,因为shell脚本有时会出现问题。

答案 1 :(得分:0)

您可以将它们直接放在root目录中。您需要具有root权限才能访问该功能,但不需要传递短语输入。