使用 echo 修改 Hosts 文件

发布于 2024-12-27 13:21:14 字数 1542 浏览 8 评论 0原文

我有两个简单的脚本,当我不希望 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰◇生如夏花灿烂 2025-01-03 13:21:14

这对我在 OSX 上有用(最后在 Yosemite 上测试过)。希望它对其他人有帮助!

sudo sh -c 'echo "127.0.0.1\twpad.company.com\n" >> /etc/hosts'

来自 Macworld 论坛

实际的解释是 sudo 以 root 身份调用子 shell,并仅将其第一个参数传递给该子 shell 来运行。

一旦命令完成,子 shell 就会退出,并且它的标准输出将通过管道传输到 >>。这会尝试打开文件参数并将其作为原始 UID 附加到其文件参数中,但由于缺乏权限而失败。

因此,解决方案是传递整个命令行,包括任何重定向,以便将整个命令行作为一个参数传递给 sudo。

This worked for me on OSX (last tested on Yosemite.) Hope it helps someone else!

sudo sh -c 'echo "127.0.0.1\twpad.company.com\n" >> /etc/hosts'

From the Macworld forums:

The actual explanation is that sudo invokes a subshell as root, and passes only it's first arg to that subshell to run.

Once the command finishes the subshell exits, and it's standard out is piped into >>. This attempts to open up and append to it's file argument as the original UID, which fails due to lack of privilege.

So, the solution is to pass the entire command line, including any redirects, so the whole thing is passed to sudo as one arg.

生活了然无味 2025-01-03 13:21:14

正在运行的 echo 版本不支持 -e。当您使用 sudo 时,您会得到 /bin/echo 而不是 shell 的内置 echo。使用 printf 代替:

echo $password | sudo -S printf "127.0.0.1\twpad.company.com\n" >> /etc/hosts

另外,请参阅 Jaypal 的评论中有关重定向和 sudo 的链接问题。

The version of echo that is being run doesn't support -e. When you use sudo you get /bin/echo rather than the shell's builtin echo. Use printf instead:

echo $password | sudo -S printf "127.0.0.1\twpad.company.com\n" >> /etc/hosts

Also, see the question linked to in Jaypal's comment regarding redirection and sudo.

墨小沫ゞ 2025-01-03 13:21:14

编辑或创建文件“/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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文