•这是要通过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
的情况下从控制台执行脚本时,不会出现其他提示:
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
您不应该在同时使用sudo
的同时使用with administrator privileges
;这是不必要的,并且会造成安全漏洞。但是,由于您已经更改了sudoers文件,因此可以尝试以下操作:
do shell script "sudo ~/Desktop/static-routes.sh"
像这样将sudo
放在前面可能会提示AppleScript做正确的事情。
请参阅Technote 2065以获取更多信息。