Create batch script to edit host file by network connection

限于喜欢 提交于 2019-12-25 03:12:27

问题


I want to use this batch script to add new entries into my host file automatically by using windows batch.

I want to edit host file only when i m in office. I want to say like that: if(network name=='OfficeWifi') do changes...

@echo off

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

exit

thx for your help


回答1:


You can get the network name (SSID) of the currently connected wireless network using the following batch file:

for /f "tokens=3" %%a in ('netsh wlan show interface ^| findstr /r "^....SSID"' ) do @echo %%a

So your batch file would look like:

@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 FOR /F, NETSH (Network Shell)




回答2:


to make it simple you could just add :

@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%


来源:https://stackoverflow.com/questions/30798355/create-batch-script-to-edit-host-file-by-network-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!