Windows 批处理:如何添加主机条目?
我想使用此批处理脚本通过使用 windows 批处理。
不幸的是,该脚本仅向主机文件添加一行,当我以管理员身份运行该脚本时也是如此,那么出了什么问题呢?
@echo off
set hostspath=%windir%\System32\drivers\etc\hosts
echo 62.116.159.4 ns1.intranet.de >> %hostspath%
echo 217.160.113.37 ns2.intranet.de >> %hostpath%
echo 89.146.248.4 ns3.intranet.de >> %hostpath%
echo 74.208.254.4 ns4.intranet.de >> %hostpath%
exit
I want to use this batch script to add new entries into my host file automatically by using windows batch.
Unfortunately, the script just adds one single line to the hosts file, also when i run the script as a administrator, so what's wrong?
@echo off
set hostspath=%windir%\System32\drivers\etc\hosts
echo 62.116.159.4 ns1.intranet.de >> %hostspath%
echo 217.160.113.37 ns2.intranet.de >> %hostpath%
echo 89.146.248.4 ns3.intranet.de >> %hostpath%
echo 74.208.254.4 ns4.intranet.de >> %hostpath%
exit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我会这样做,这样如果脚本运行多次,您就不会得到重复的条目。
I would do it this way, so you won't end up with duplicate entries if the script is run multiple times.
简单的错字。
hostspath
与hostpath
;)Plain typo.
hostspath
vshostpath
;)这是我对上面@rashy 的修改。该脚本执行以下操作:
这是脚本:
Here is my modification of @rashy above. The script does the following:
This is the script:
创建一个新的 addHostEntry.bat 文件,其中包含以下内容:
希望这有帮助!
Create a new addHostEntry.bat file with the following content in it:
Hope this helps!
我添加这个答案是为了防止其他人想要将主机条目集存储在格式类似于普通主机文件的 txt 文件中。这将查找制表符分隔符。这是基于@Rashy 和@that0n3guy 的答案。可以注意到 FOR 命令周围的差异。
示例“storedhosts.txt”(制表符分隔)
I am adding this answer in case someone else would like to store the host entry set in a txt file formatted like the normal host file. This looks for a TAB delimiter. This is based off of the answers from @Rashy and @that0n3guy. The differences can be noticed around the FOR command.
Example "storedhosts.txt" (tab delimited)
有时我必须在家工作并通过 VPN 连接到办公室。内部域名应该解析到家里不同的IP。在办公室和家里之间有几个名字必须更改。例如:
我的解决方案是创建两个文件:C:\WINDOWS\system32\drivers\etc\hosts-home 和 C:\WINDOWS\system32\drivers\etc\hosts-office。它们中的每一个都包含一组名称到 IP 的映射。从管理员PowerShell,当我在办公室工作时,执行
当我到家时,执行
Sometime I have to work from home and connect to office through vpn. Internal domain names should be resolved to different IPs at home. There are several names that have to be changed between office and home. For example:
My solution is to create two files: C:\WINDOWS\system32\drivers\etc\hosts-home and C:\WINDOWS\system32\drivers\etc\hosts-office. Each of them contains set of name-to-IP mapping. From Administrator PowerShell, When I work at the office, execute
When I arrive at home, execute
我写的脚本效果很好。
Well I write a script which works very well.