Bash 如何使自动机运行需要sudo密码的Shell脚本?
我使用下面的脚本在每次运行时随机更改mac地址Bash 如何使自动机运行需要sudo密码的Shell脚本?,bash,macos,shell,terminal,automator,Bash,Macos,Shell,Terminal,Automator,我使用下面的脚本在每次运行时随机更改mac地址 openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//' | xargs sudo ifconfig en0 ether ifconfig en0 | grep ether 我想让自动机帮我做这件事。当我运行这个Shell脚本时,它会成功运行,但当我实际打开终端并运行时 ifconfig en0 | grep ether 看看它是否改变了MAC地址,我发现它没有 若我手动将这样的脚本输入到终端,它就
openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//' | xargs sudo ifconfig en0 ether
ifconfig en0 | grep ether
我想让自动机帮我做这件事。当我运行这个Shell脚本时,它会成功运行,但当我实际打开终端并运行时
ifconfig en0 | grep ether
看看它是否改变了MAC地址,我发现它没有
若我手动将这样的脚本输入到终端,它就可以完美地工作。
我该怎么办?试着创建一个
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脚本有时会出现问题。您可以将它们直接放在
根目录中。您需要具有root权限才能访问该函数,但不需要密码输入 请注意,如果需要采用自动机工作流的形式,可以使用“运行Applescript”操作将其包括在工作流中。这有点间接(使用Automator来运行运行shell脚本的AppleScript),但这是我发现的从Automator以root身份运行命令的最简单方法。这仍然有效吗?我这样做的时候被拒绝了this@spracketchip:是的,我刚才试过(macOS 10.13.2),它对我很有效。