原创

windows 使用BAT批量修改网卡的网关

温馨提示:
本文最后更新于 2022年12月31日,已超过 721 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

有时候我们需要批量去修改网关,手工修改的话很费事,下面是一个BAT脚本去批量修改网关IP地址的实例:

for /f "tokens=2 delims=:" %%i in ('netsh interface ipv4 show address "Ethernet1" ^| findstr "IP 地址"') do set ip=%%i
for /f "tokens=2 delims=:" %%g in ('netsh interface ipv4 show address "Ethernet1" ^| findstr "默认网关"') do set gataway=%%g
set "gataway=%gataway: =%"
set "ip=%ip: =%"

if "%ip:~0,3%"=="169" goto noip
if "%ip:~0,3%"==" =" goto noip
if "%gataway:~0,3%"=="192" goto havegataway
if "%gataway:~0,3%"==" =" goto setgataway
goto end

:noip
eventcreate /T ERROR /L APPLICATION /ID 110 /D "目前没有IP地址跳过设置:%ip%"
goto end

:havegataway
eventcreate /T INFORMATION /L APPLICATION /ID 110 /D "接口已经有网关地址,放弃更新:%ip%"
goto end

:setgataway
netsh interface ip set address Ethernet1 static %ip% 255.255.255.0 192.168.1.254
netsh advfirewall set currentprofile state off
eventcreate /T INFORMATION /L APPLICATION /ID 110 /D "网关更新成功,IP地址为:%ip%"
goto end

:end
echo "end"

正文到此结束
本文目录