我是个新手,所以请耐心一点。
我想写一个批处理文件来替换etc/hosts文件,这取决于我登录时所在的VLAN。ie:当我登录并获得xxx.xxx.102.xxx本地IP时,我希望使用hosts1文件。当我登录并获得xxx.xxx.103.xxx本地IP时,我希望使用hosts2文件。
因此,基本上根据ipconfig的输出,扫描"102“和"103”,当该数字出现在输出中时,它可以替换Windows目录中的hosts文件。
想法?
发布于 2014-01-20 23:30:59
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"
)将工作,如果您有管理员权限和您的主机文件是一个and hosts101 hosts102 ...
发布于 2014-01-21 00:05:08
你可以试试这个。我在这里使用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"https://stackoverflow.com/questions/21237635
复制相似问题