2015-06-12 62 views
0

我想用這個批處理腳本新條目通過使用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對您有所幫助

+1

這將多次,如果你運行它多次添加的條目。您還想在不在該網絡時刪除條目。你不想自己編寫腳本。檢查http://superuser.com/questions/663183/change-hosts-file-based-on-connection-windows例如。 – CodeCaster

回答

1

你可以得到使用下面的批處理文件的當前連接的無線網絡的網絡名稱(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 

來源FOR /FNETSH (Network Shell)

1

,使之簡單,你只需要添加:

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

如何清除主機文件第一?想到的 – Mennan

+0

第一件事就是有2個不同的版本,這取決於是否錯誤級別爲1或不hosts文件的腳本複製1。這樣可以節省您的格式問題HOSTFILE中的麻煩和需要後,將其清除。 –