[执行脚本时使用的用户是AppleScript

问题描述 投票:0回答:1

•这是要通过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助手enter image description here

•在没有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使用哪个用户来执行脚本?可以修改吗?

macos routes applescript sudo visudo
1个回答
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

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

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

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

请参阅Technote 2065以获取更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.