Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.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 创建批处理脚本以通过网络连接编辑主机文件_Windows_Batch File_Cmd_Network Programming - Fatal编程技术网

Windows 创建批处理脚本以通过网络连接编辑主机文件

Windows 创建批处理脚本以通过网络连接编辑主机文件,windows,batch-file,cmd,network-programming,Windows,Batch File,Cmd,Network Programming,我想使用此批处理脚本通过使用windows批处理将新条目自动添加到主机文件中 我只想在办公室编辑主机文件。我想这样说: 如果(网络名称=='OfficeWifi')进行更改 @echo off set hostspath=%windir%\System32\drivers\etc\hosts // if(network name=='OfficeWifi') echo 81.155.145.48 ns1.intranet.de >> %hostspath% exit thx为您

我想使用此批处理脚本通过使用windows批处理将新条目自动添加到主机文件中

我只想在办公室编辑主机文件。我想这样说: 如果(网络名称=='OfficeWifi')进行更改

@echo off

set hostspath=%windir%\System32\drivers\etc\hosts
// if(network name=='OfficeWifi')
echo 81.155.145.48 ns1.intranet.de >> %hostspath%

exit

thx为您提供帮助

要使其变得简单,您只需添加:

@echo off

set hostspath=%windir%\System32\drivers\etc\hosts
ping "name of office DC" 
if errorlevel 1 quit    
if not errorlevel 1 echo 81.155.145.48 ns1.intranet.de >> %hostspath%

您可以使用以下批处理文件获取当前连接的无线网络的网络名称(SSID):

for /f "tokens=3" %%a in ('netsh wlan show interface ^| findstr /r "^....SSID"' ) do @echo %%a
因此,您的批处理文件如下所示:

@echo off
set hostspath=%windir%\System32\drivers\etc\hosts
for /f "tokens=3" %%a in ('netsh wlan show interface ^| findstr /r "^....SSID"' ) do (
  if "%%a"=="OfficeWifi" echo 81.155.145.48 ns1.intranet.de >> %hostspath%
)
exit


Sources,

如果您多次运行该条目,将多次添加该条目。您还希望在不在该网络上时删除该条目。你不想自己编写脚本。例如,检查一下。首先想到的是,根据错误级别是否为1,让脚本复制2个不同版本的hostsfile的1。这样就省去了主机文件中格式问题的麻烦,并且在以后需要清除它。