有时候我们需要批量去修改网关,手工修改的话很费事,下面是一个BAT脚本去批量修改网关IP地址的实例:
for /f "tokens=2 delims=:" %%i in ('netsh interface ipv4 show address "Ethernet1" ^| findstr "IP 地址"') do set ip=%%ifor /f "tokens=2 delims=:" %%g in ('netsh interface ipv4 show address "Ethernet1" ^| findstr "默认网关"') do set gataway=%%gset "gataway=%gataway: =%"set "ip=%ip: =%"
if "%ip:~0,3%"=="169" goto noipif "%ip:~0,3%"==" =" goto noipif "%gataway:~0,3%"=="192" goto havegatawayif "%gataway:~0,3%"==" =" goto setgatawaygoto end
:noipeventcreate /T ERROR /L APPLICATION /ID 110 /D "目前没有IP地址跳过设置:%ip%"goto end
:havegatawayeventcreate /T INFORMATION /L APPLICATION /ID 110 /D "接口已经有网关地址,放弃更新:%ip%"goto end
:setgatawaynetsh interface ip set address Ethernet1 static %ip% 255.255.255.0 192.168.1.254netsh advfirewall set currentprofile state offeventcreate /T INFORMATION /L APPLICATION /ID 110 /D "网关更新成功,IP地址为:%ip%"goto end
:endecho "end"