Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 基于IP的“主机”文件_Windows_Batch File_Dns_Ip_Hosts - Fatal编程技术网

Windows 基于IP的“主机”文件

Windows 基于IP的“主机”文件,windows,batch-file,dns,ip,hosts,Windows,Batch File,Dns,Ip,Hosts,我是新手,所以请容忍我一点 我想编写一个批处理文件来替换etc/hosts文件,具体取决于我登录时所在的VLAN。 ie:当我登录并获得xxx.xxx.102.xxx本地IP时,我想使用hosts1文件。当我登录并获得xxx.xxx.103.xxx本地IP时,我想使用hosts2文件 因此,基本上基于ipconfig的输出,扫描102和103,当输出中有这个数字时,它可以替换Windows目录中的hosts文件 想法 如果您具有管理员权限,并且您的主机文件是anemd hosts101 host

我是新手,所以请容忍我一点

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

因此,基本上基于ipconfig的输出,扫描102和103,当输出中有这个数字时,它可以替换Windows目录中的hosts文件

想法

如果您具有管理员权限,并且您的主机文件是anemd hosts101 hosts102…

您可以尝试此操作。我在这里使用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"

你不能在路由器配置中将一个静态本地IP分配给你的MAC地址吗?我不确定regexp是否能处理IP地址中的单个数字。@foxidrive这个Regex显然不能测试IPv4的有效性。但在我看来,它可以在输出中找到有效的IPv4。为什么你认为,它不会处理单个数字?[0-9][0-9]*与ERE中的[0-9]+相同,即一个或多个。
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"