【问题标题】:Which user is AppleScript using when executing scripts执行脚本时 AppleScript 使用哪个用户
【发布时间】:2020-05-05 06:59:45
【问题描述】:

• 下面是通过 AppleScript 执行的脚本:

bash-3.2$ cd /Users/jack/Desktop/
bash-3.2$ ls -l | grep static
-rwxrwxrwx   1 jack  admin      65  5 May 08:10 static-routes.sh
bash-3.2$ cat static-routes.sh 
#!/bin/bash
sudo route -n add -net 192.168.3.0/24 172.16.254.134
~                                                         

• AppleScript 包含以下内容:

do shell script "~/Desktop/static-routes.sh"

• 从 AppleScript 中执行脚本时,通过单击“运行”按钮,弹出窗口说:

脚本错误 sudo:需要终端才能读取密码; 要么使用 -S 选项从标准输入中读取,要么 配置 askpass 助手

• 在没有sudo 的情况下从控制台执行脚本时,不会出现其他提示:

bash-3.2$: Desktop jack$ ./static-routes.sh 
add net 192.168.3.0: gateway 172.16.254.134

• 这是来自/etc/sudoers 的sn-p:

bash-3.2$ sudo visudo
# root and users in group wheel can run anything on any machine as any user
root            ALL = (ALL) ALL
%admin          ALL = (ALL) ALL
jack ALL = (ALL) NOPASSWD: /Users/jack/Desktop/static-routes.sh

## Read drop-in files from /private/etc/sudoers.d
## (the '#' here does not indicate a comment)
#includedir /private/etc/sudoers.d
Defaults timestamp_timeout=60

问题:

• 为什么会出现此错误,因为我已将脚本显式添加到 sudoers 文件中,以便通过 sudo 无需密码提示即可执行?

• AppleScript 使用哪个用户来执行脚本?可以修改吗?

【问题讨论】:

标签: macos routes applescript sudo visudo


【解决方案1】:

运行需要 AppleScript 权限的命令,您需要通过添加 administrator privileges 键来指定,如下所示:

-- this will presented a standard authorization dialog
do shell script "~/Desktop/static-routes.sh" with administrator privileges

-- this will specifies an administrator account and password
-- (though note, the password will be visible as plain text in the script)
do shell script "~/Desktop/static-routes.sh" with administrator privileges user name XXXX password YYYY

您不应在使用with administrator privileges 的同时使用sudo;这是不必要的,并且会造成安全漏洞。但是,由于您已经更改了 sudoers 文件,您可以试试这个:

do shell script "sudo ~/Desktop/static-routes.sh"

像这样将sudo 放在前面可能会提示 AppleScript 做正确的事情。

更多信息请参见Technote 2065

【讨论】:

  • 将脚本更改为do shell script "sudo ~/Desktop/static-routes.sh" 后,它开始按预期工作。谢谢!
猜你喜欢
  • 2011-02-08
  • 2017-02-03
  • 1970-01-01
  • 2021-07-07
  • 2022-12-11
  • 1970-01-01
  • 2011-04-09
  • 2012-07-02
  • 1970-01-01
相关资源
最近更新 更多