基于IP的“主机”文件

时间:2014-01-20 15:19:40

标签: windows batch-file dns ip hosts

我是新手,所以请跟我一点。

我想编写一个可以替换etc / hosts文件的批处理文件,具体取决于我登录时所处的VLAN。 即:当我登录并且我有xxx.xxx.102.xxx本地IP时,我想使用hosts1文件。当我登录并且我有xxx.xxx.103.xxx本地IP时,我想使用hosts2文件。

所以基本上基于ipconfig的输出,扫描它为“102”和“103”,当该数字在输出中时,它可以替换Windows目录中的hosts文件。

想法?

2 个答案:

答案 0 :(得分:0)

for %%a in (102 102 103) do (

  ipconfig | find "xxx.xxx.%%a.xxx" >nul 2>&1 && copy /y hosts%%a "%SystemRoot%\system32\drivers\etc\hosts"
)
如果您具有管理员权限并且您的主机文件被命名为主机101主机102 ...

,则

将起作用

答案 1 :(得分:0)

你可以试试这个。我在这里使用findstr的正则表达式功能:

ipconfig | findstr /er "[0-9][0-9]*.[0-9][0-9]*.102.[0-9][0-9]*" && copy hosts1 "%windir%\system32\drivers\etc\hosts"
ipconfig | findstr /er "[0-9][0-9]*.[0-9][0-9]*.103.[0-9][0-9]*" && copy hosts2 "%windir%\system32\drivers\etc\hosts"