AppleScript 在执行脚本时使用哪个用户

Which user is AppleScript using when executing scripts

• 这是通过 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 中执行脚本时,通过单击 "Run" 按钮,弹出 window 说:

Script Error sudo: a terminal is required to read the password; Either use the -S option to read from standard input or configure an askpass helper

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

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

• 这是来自 /etc/sudoers 的片段:

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 使用哪个用户来执行脚本?是否可以修改?

运行 一个需要 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