【问题标题】:How to make Automator run Shell Script that requires sudo password?如何让 Automator 运行需要 sudo 密码的 Shell 脚本?
【发布时间】:2017-09-30 13:55:38
【问题描述】:

我使用下面的这个脚本在每次运行时随机更改 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 地址,我发现它没有。

如果我手动将此类脚本输入到终端,它会完美运行。 我该怎么办?

【问题讨论】:

    标签: bash macos shell terminal automator


    【解决方案1】:

    尝试创建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 通常更适合运行 Applescript,因为 shell 脚本有时会出现问题。

    【讨论】:

    • 请注意,如果它需要采用 Automator 工作流程的形式,您可以使用“运行 Applescript”操作将其包含在工作流程中。这有点间接(使用 Automator 运行运行 shell 脚本的 AppleScript),但这是我发现从 Automator 以 root 身份运行命令的最简单方法。
    • 这还能用吗?执行此操作时我的权限被拒绝
    • @spracketchip:是的,我刚刚尝试(macOS 10.13.2)时它工作得很好。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 2014-09-29
      • 2023-03-23
      • 2016-02-29
      • 2018-01-26
      • 1970-01-01
      • 2011-12-28
      相关资源
      最近更新 更多