懒,就要懒到底; 工作中遇到一个坑点,存在两个网络环境,需要配置不同的IP才能访问,今天终于忍无可忍,把它自动化了,在未自动化前,我需要经历『打开网络和共享中心』-> 『更改适配器设置』->『右键本地连接属性』-> 『双击Ipv4』-> 配置IP、子网掩码、网关,之后再插拔网线;在自动化之后,我只需要插拔网线,脚本会自动根据网络类型设置相应的IP。现在分享脚本如下

:: The script use to auto switch network ip 
:: Author: smallmuou

@echo off

:Begin

for /f "tokens=2 delims=:" %%i in ('ipconfig^|findstr 默认网关') do set gw=%%i
ping -n 1 %gw% >nul

if errorlevel 1 goto AutoConfig 
if errorlevel 0 goto Continue

:AutoConfig

if "%gw%" == " 192.168.12.1" ( goto ConfigStarnet ) else goto Config161


:ConfigStarnet

echo %time% switch to starnet ...

netsh interface ip set address 本地连接 static 192.168.60.152 255.255.255.0 192.168.60.1 
goto Continue

:Config161

echo %time% switch to 161 ...

netsh interface ip set address 本地连接 static 192.168.12.152 255.255.255.0 192.168.12.1
goto Continue

:Continue

echo %time% checking network ...

::delay
ping -n 10 127.0.0.1 >nul 
goto Begin