使用 echo 修改 Hosts 文件
我有两个简单的脚本,当我不希望 Cisco AnyConnect 在每次登录/网络转换时尝试连接时,它们可以启用/禁用 Cisco AnyConnect。一切都很好,但我也想在主机文件中添加一行。我对大多数命令使用“echo $password | sudo -S”的原因是因为该脚本是从 Mac OS X 中的脚本菜单运行的。终端窗口不会打开以解决 sudo 密码提示。
#!/bin/bash
#Start_AnyConnect.command
password=`/usr/bin/osascript <<EOT
with timeout of (30 * 60) seconds
tell application "Finder"
activate
set myReply to text returned of (display dialog "Enter your password to authorize AnyConnect startup script" default answer "" with hidden answer)
end tell
end timeout
EOT`
echo $password | sudo -S echo -e "127.0.0.1\twpad.company.com" >> /etc/hosts
sleep 2
echo $password | sudo -S mv "/Library/LaunchAgents_Disabled/com.cisco.anyconnect.gui.plist" "/Library/LaunchAgents/com.cisco.anyconnect.gui.plist"
echo $password | sudo -S mv "/Library/LaunchDaemons_Disabled/com.cisco.anyconnect.vpnagentd.plist" "/Library/LaunchDaemons/com.cisco.anyconnect.vpnagentd.plist"
echo $password | sudo -S launchctl load /Library/LaunchDaemons/com.cisco.anyconnect.vpnagentd.plist
sleep 5
open /Applications/Cisco/Cisco\ AnyConnect\ Secure\ Mobility\ Client.app
exit 0
我遇到的问题是
echo $password | sudo -S echo -e "127.0.0.1\twpad.company.com" >> /etc/hosts
附加“-e 127.0.0.1\twpad.company.com
”而不是“127.0.0.1 wpad.company.com
”主机文件。
如果我单独运行以下命令,它会按预期工作:
sudo echo -e "127.0.0.1\twpad.company.com" >> /etc/hosts
还有其他方法可以做到这一点吗?
谢谢你!
I have two simple script that enables/disables Cisco AnyConnect when I don't want it trying to connect on each login/network transition. All was fine and dandy, but I wanted to add a line to the hosts file as well. The reason I'm using "echo $password | sudo -S" for most of the commands is because this script is being run from the script menu in Mac OS X. The terminal window does not open to address sudo password prompts.
#!/bin/bash
#Start_AnyConnect.command
password=`/usr/bin/osascript <<EOT
with timeout of (30 * 60) seconds
tell application "Finder"
activate
set myReply to text returned of (display dialog "Enter your password to authorize AnyConnect startup script" default answer "" with hidden answer)
end tell
end timeout
EOT`
echo $password | sudo -S echo -e "127.0.0.1\twpad.company.com" >> /etc/hosts
sleep 2
echo $password | sudo -S mv "/Library/LaunchAgents_Disabled/com.cisco.anyconnect.gui.plist" "/Library/LaunchAgents/com.cisco.anyconnect.gui.plist"
echo $password | sudo -S mv "/Library/LaunchDaemons_Disabled/com.cisco.anyconnect.vpnagentd.plist" "/Library/LaunchDaemons/com.cisco.anyconnect.vpnagentd.plist"
echo $password | sudo -S launchctl load /Library/LaunchDaemons/com.cisco.anyconnect.vpnagentd.plist
sleep 5
open /Applications/Cisco/Cisco\ AnyConnect\ Secure\ Mobility\ Client.app
exit 0
The problem I'm having is that
echo $password | sudo -S echo -e "127.0.0.1\twpad.company.com" >> /etc/hosts
appends "-e 127.0.0.1\twpad.company.com
" in stead of "127.0.0.1 wpad.company.com
" to the hosts file.
If I run the following command by itself it works as expected:
sudo echo -e "127.0.0.1\twpad.company.com" >> /etc/hosts
Is there another way to do this?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这对我在 OSX 上有用(最后在 Yosemite 上测试过)。希望它对其他人有帮助!
来自 Macworld 论坛:
This worked for me on OSX (last tested on Yosemite.) Hope it helps someone else!
From the Macworld forums:
正在运行的
echo
版本不支持-e
。当您使用sudo
时,您会得到/bin/echo
而不是 shell 的内置echo
。使用printf
代替:另外,请参阅 Jaypal 的评论中有关重定向和
sudo
的链接问题。The version of
echo
that is being run doesn't support-e
. When you usesudo
you get/bin/echo
rather than the shell's builtinecho
. Useprintf
instead:Also, see the question linked to in Jaypal's comment regarding redirection and
sudo
.编辑或创建文件“/etc/hosts.ac”以添加所需的主机条目。当您启动 AnyConnect 时,该文件将替换“/etc/hosts”。
不需要脚本附加。
Edit or create the file '/etc/hosts.ac' to add your desired host entries. When you start AnyConnect, that file will replace '/etc/hosts'.
No scripted appending will be needed.