Windows之多场景IP自动设置bat脚本

  • A+
所属分类:Windows
摘要

为了方便在多长场景下快速切换IP和IP获取类型就随便写了个bat的脚本。

@echo off
REM 需要设置的网络,一般为"本地连接"或者"无线网络连接"
set name=以太网

REM ################### START ###################
echo.
echo.
set /p type=请选择设置方式, 1:静态, 2:DHCP, 3:自定义 : 
if /i %type%==1 goto :STATIC_IP
if /i %type%==2 goto :DHCP_IP
if /i %type%==3 goto :STATIC_CONFIG
:STATIC_IP
echo.
set /p staticNO=静态IP场所, 1:公司, 2:宿舍, 3:家里 : 
if /i %staticNO%==1 goto :STATIC_IP_COMPANY
if /i %staticNO%==2 goto :STATIC_IP_DORM
if /i %staticNO%==3 goto :STATIC_IP_HOME

REM 设置静态IP
:STATIC_IP_CONFIG
netsh -c interface ip set address name="%name%" source=static addr=%ipaddress% mask=%mask% gateway=%gateway% gwmetric=0
goto :PRINT_CONFIG
REM netsh -c interface ip add address name="%name%" addr=%ipaddress2% mask=%mask2% gateway=%gateway2% gwmetric=1

REM 设置动态IP
:DHCP_IP
echo 正在进行动态IP设置,请稍等...
netsh interface ip set address name=%name% source=dhcp
goto :SETEND

REM 打印设定内容
:PRINT_CONFIG
echo. IP地址 = %ipaddress% .完成设置
echo. 子掩码 = %mask% .完成设置
echo. 网关 = %gateway% .完成设置
goto :DNS_CONFIG

REM 配置为公司IP
:STATIC_IP_COMPANY
echo.
set /p CompanyIP=选择公司网关, 1:223, 2:254 : 
if /i %CompanyIP%==1 goto :COMPANY_223
if /i %CompanyIP%==2 goto :COMPANY_254

REM #### 设置静态IP ####
:COMPANY_223
set ipaddress=10.50.200.112
set mask=255.255.255.0
set gateway=10.50.200.223
goto :STATIC_IP_CONFIG

:COMPANY_254
set ipaddress=10.50.200.200
set mask=255.255.255.0
set gateway=10.50.200.254
goto :STATIC_IP_CONFIG

REM 配置为宿舍IP
:STATIC_IP_DORM
set ipaddress=172.16.41.252
set mask=255.255.0.0
set gateway=172.16.0.1
goto :STATIC_IP_CONFIG
REM 配置为家里IP
:STATIC_IP_HOME
set ipaddress=172.16.41.252
set mask=255.255.0.0
set gateway=172.16.0.1
goto :STATIC_IP_CONFIG
:STATIC_CONFIG
echo.
set /p ipaddress=请输入指定的静态IP(例如:172.16.1.100): 
set /p mask=请输入子网掩码(例如:255.255.0.0): 
set /p gateway=请输入网关地址(例如:172.16.0.1): 
goto :STATIC_IP_CONFIG

:DNS_CONFIG
echo.
set /p dnsconfig=指定DNS, 1:谷歌, 2:阿里, 3:114, 4:自动 : 
if /i %dnsconfig%==1 goto :DNS_GOOGLE
if /i %dnsconfig%==2 goto :DNS_ALI
if /i %dnsconfig%==3 goto :DNS_114
if /i %dnsconfig%==4 goto :DNS_AUTO

REM 设定谷歌DNS
:DNS_GOOGLE
set dns1=8.8.4.4
set dns2=8.8.8.8
goto :DNS_SET
REM 设定阿里DNS
:DNS_ALI
set dns1=223.5.5.5
set dns2=223.6.6.6
goto :DNS_SET
REM 设定114DNS
:DNS_114
set dns1=114.114.115.115
set dns2=114.114.114.114
goto :DNS_SET

REM 设定自动获取DNS
:DNS_AUTO
netsh interface ip set dns name=%name% source=dhcp register=PRIMARY
netsh interface ip set wins name=%name% source=dhcp

REM DNS设置生效
:DNS_SET
netsh -c interface ip set dns name="%name%" static %dns1% >nul
netsh -c interface ip add dns name="%name%" addr=%dns2% index=2 >nul
echo. 首选DNS = %dns1% .完成设置
echo. 备用DNS = %dns2% .完成设置
goto :SETEND

REM 结束
:SETEND
echo.
echo.
echo.
pause
lookback

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: