在OS X上使用expect运行shell脚本作为admin

时间:2015-02-23 19:32:46

标签: bash shell expect osx-yosemite

我试图让非管理员用户在需要root权限的共享环境中运行脚本。我打算构建一个AppleScript或Automator应用程序来调用expect来传递登录凭据(是的,我知道危险,它只会执行)。但是我让登录工作,但我试图运行的shell脚本似乎没有执行,因为它立即返回。我是新手,我相信我的脚本中有错误。任何想法?

#!/usr/bin/expect
set timeout 60
set password "MyPass"
set command "bash /Applications/mampstack-5.4.36-0/ctlscript.sh start"
eval spawn login admin
expect "assword:"
send "$password\n"
expect "$"
send  "$command\n"
expect "$"
send exit
expect "$"
send exit

1 个答案:

答案 0 :(得分:2)

不要这样做。相反,请修改/etc/sudoers以添加以下行(或者,在MacOS的默认配置中,您可以使用以下内容在/etc/sudoers.d中创建文件):

# allow use by GUI apps (AppleScript, Automator), which don't have a TTY
Defaults:%staff !requiretty

# on MacOS, all humans are members of the staff group
%staff ALL=(ALL) NOPASSWD: /Applications/mampstack-5.4.36-0/ctlscript.sh start

...然后,运行AppleScript或Automator代码:

sudo -u admin /Applications/mampstack-5.4.36-0/ctlscript.sh start

...根本不需要密码。