@echo off
mode con cols=70 lines=38
color a
title D-小苏-学习-Bat-快速修改本地IP地址
:ShowReadme
cls
echo.
echo *********************学习-Bat-快速修改本地IP地址**********************
echo.
echo 实现功能(以下情况下测试通过):
echo 1.多连接名称:自动获取网络连接名称
echo 2.多连接属性:有线连接,无线连接,虚拟连接
echo 3.多操作系统:Windows XP,Windows 7 / 8.1
echo 4.多网络连接:无线连接2个,有线连接1个,VMware连接2个
echo 5.区域的DNS:判断计算机可以连接互联网则采用当前DNS设置
echo 6.空格名称:VMware Virtual Ethernet Adapter for VMnet1
echo 7.优化设置:优化选择步骤,添加选择序号,保存配置内容
echo 8.设置方法:可转换Netsh interface ip set 和WMIC nicconfig
echo 9.获取优化:判断网关值,存储有效的网关值以打开路由器设置页
echo.
echo *********************学习-Bat-快速修改本地IP地址**********************
ping -n 1 127.1 >nul
:Set_localTemp
setlocal enabledelayedexpansion
ver | find "6." > NUL &&(cls&echo. &echo -------------------------------当前系统为 Windows 7以上系统 &Set "WindowsVer=Windows 7" &goto :Set_NetConnectionID)
cls&echo. &echo -------------------------------当前系统不是 Windows 7以上系统 &Set "WindowsVer=WindowsXP" &goto :Set_NetConnectionID
:Set_NetConnectionID
rem 获取设置网络连接名称
cls
echo.
echo *************选择网络连接名称*****************IN %WindowsVer%**********
echo.
set "str=DefaultIPGateway^,IPAddress^,DNSServerSearchOrder^,IPSubnet^,DHCPEnabled"
for /f "skip=1tokens=1*" %%i in ('wmic Nic where "Manufacturer<>'Microsoft' and NetConnectionStatus='2'" get Index^,NetConnectionID^') do (
for /f "tokens=1* delims=:" %%j in ("%%j") do (
set $%%i=%%j
Echo %%i_%%j>>适配器_Index_Name.txt
echo.
echo 适配器_ID_Index: %%i
echo 适配器_名_NName: %%j
for /f "tokens=1-3delims={}," %%a in ('wmic Nicconfig where "Index='%%i'" get %str% /value') do (
for /f %%c in (%%c) do if /i "%%a" == "DNSServerSearchOrder=" set "DNS2=%%c"
for /f %%c in ("%%a%%~b") do set "%%c"
)
if /i "!DHCPEnabled!" == "TRUE" (
echo 适配器_IP_模式 : 自动
) else echo 适配器_IP_模式 : 手动
IF not defined Net_IP set Net_IP=!IPAddress!
echo 适配器_IP_地址 : !IPAddress! Net_IP_地址 : !Net_IP!
IF not defined Net_MASK set Net_MASK=!IPSubnet!
Rem 判断是否存在值,以解决之后Net_MASK赋值为空的情况,保证Net_MASK存在值
echo 适配器_子网掩码: !IPSubnet! Net_子网掩码: !Net_MASK!
IF not defined Net_gateway set Net_gateway=!DefaultIPGateway!
echo 适配器_默认网关: !DefaultIPGateway! Net_默认网关: !Net_gateway!
IF not defined Net_dnsPra set Net_dnsPra=!DNSServerSearchOrder!
echo 适配器_首选_DNS: !DNSServerSearchOrder! Net_首选_DNS: !Net_dnsPra!
IF defined dns2 set Net_dns2=!DNS2! &echo 适配器_配用_DNS :!DNS2! Net_配用_DNS: !Net_dns2!
)
)
echo.
echo.
echo ************************选择网络连接名称*****************************
(for /f "tokens=1* delims=:" %%a in ('findstr /n .* 适配器_Index_Name.txt') do echo ID_%%a_%%b)>适配器_ID_Index_Name.txt
REM 生成选择结构,添加序号ID_,避免纯查找数字序号会关键词的查找重复
for /f "tokens=2-4 delims=_" %%a in (适配器_ID_Index_Name.txt) do (
set NetConID=%%a
set NetConnectionID_Index=%%b
set NetConnectionName_Index=%%c
echo 适配器ID:!NetConID! 标号Index:!NetConnectionID_Index! 名称:!NetConnectionName_Index!
)
echo *************选择网络连接名称*****************IN %WindowsVer%***********
:Set_Net_Ask
Echo.
echo.
set /p NetConID=选择需要配置 适配器 对应 数字ID :
if /i "%NetConID%" == "q" exit
echo.
echo.
set NetConnectionName_Index=
for /f "tokens=2-4 delims=_" %%a in ('findstr "ID_%NetConID%" 适配器_ID_Index_Name.txt') do (
setlocal enabledelayedexpansion
set NetConnectionID_Index=%%b
set NetConnectionName_Index=%%c
echo 当前选择适配器ID:!NetConID! 标号Index:!NetConnectionID_Index! 名称:!NetConnectionName_Index!
)
if "%NetConnectionName_Index%" == "" (echo 选择选项超出范畴 ... &ping -n 3 127.1>nul &goto Set_Net_Ask) else (set net_interface=%NetConnectionName_Index% &echo 名称:!net_interface! )
if exist 适配器_Index_Name.txt (del /a /f /s /q 适配器_Index_Name.txt) else echo 适配器_Index_Name.txt文件夹不存在
if exist 适配器_ID_Index_Name.txt (del /a /f /s /q 适配器_ID_Index_Name.txt) else echo 适配器_ID_Index_Name.txt文件夹不存在
ping -n 1 127.1>nul
:Set_RamdonIP
Rem 如果计算机可以连接互联网,判断网络DNS、网关是否可用,可以连接互联网则只改IP,启用当前区域DNS...
Rem 获得可上网IP前三位,设置为随机IP前三位
if "%Net_IP%" == "" (echo 设置Net_IP &set Net_IP=192.168.1.128)
for /f "tokens=1-4 delims=/." %%a in ("%Net_IP%") do (set IPa_=%%a.%%b.%%c)
set /a IPb_=%random%%%255+3
set "net_IPnew=%IPa_%.%IPb_%"
echo 随机IP前三位[%IPa_%] 生成随机IP [%net_IPnew%]
ping 127.0.0.1 -n 1 >nul 1>nul
ping www.baidu.com &echo %errorlevel%
if %errorlevel% equ 1 goto:Set_Net_Set_NoHave
if %errorlevel% equ 0 goto:Set_Net_Set_Have
:Set_Net_Set_Have
Echo 当前计算机可以连通互联网...启用当前区域DNS...
set Net_Ping=1
set Net_IP_Diy=%net_IPnew%
Rem 当前IP地址
ping -n 1 127.0.0.1>nul
goto DiyCHOICE
:Set_Net_Set_NoHave
Echo 当前计算机无法连通互联网...
set Net_Ping=0
set Net_IP_Diy=%net_IPnew%
set "Net_MASK=255.255.255.0"
set "Net_gateway=192.168.1.1"
set "Net_dnsPra=202.100.192.68"
set "Net_dns2=202.100.199.8"
ping -n 1 127.0.0.1>nul
goto DiyCHOICE
:DiyCHOICE
cls
echo.
echo ****************************IP快速切换程序***************************
echo.
echo 当前选择网络连接:
echo ID:%NetConID% 名称:%net_interface%
echo.
echo.
echo --------------1.修改为随机IP---------------------%Net_IP_Diy%
echo --------------2.设自动获取IP---------------------XXX.XXX.X.XXX
echo --------------3.手动设网关IP---------------------192.XXX.X.XXX
echo --------------4.查看网络设置---------------------%Net_IP%
echo --------------5.设置路由器页---------------------%Net_gateway%
echo.
echo --------------r.重启网卡-------------------------x.退出程序---
echo.
echo.
echo **** IN %WindowsVer% *******************************Tools By S34205****
echo.
echo.
echo.
if "%Net_Ping%" == "1" (echo 当前计算机可以连接互联网,启用当前区域DNS...) else (echo 当前计算机无法连接互联网,可能需要设置您区域DNS....)
echo 随机的IP:[%Net_IP_Diy%]
echo 子网掩码:[%Net_MASK%]
echo 默认网关:[%Net_gateway%]
echo 首选DNS:[%Net_dnsPra%]
IF defined dns2 echo 备用DNS:[%Net_dns2%]
echo.
echo.
ping -n 1 127.1 >nul
echo.
:Diysetchoice
set /p SetChoice_C= 请输入操作:
Echo.
Echo.
IF NOT "%SetChoice_C%"=="" SET SetChoice_C=%SetChoice_C:~0,1%
if /i "%SetChoice_C%"=="1" goto Static_ip
if /i "%SetChoice_C%"=="2" goto DHCP_ip
if /i "%SetChoice_C%"=="3" goto Diy_ip
if /i "%SetChoice_C%"=="4" goto Look_ip
if /i "%SetChoice_C%"=="5" goto Set_Moden_Web
if /i "%SetChoice_C%"=="r" goto rebootnet
if /i "%SetChoice_C%"=="x" goto END
echo 输入的选择超出范围...
ping -n 3 127.1>nul
goto DiyCHOICE
:Static_ip
echo.
echo 设置随机IP:[%Net_IP_Diy%] ...
REM netsh interface ip set address name="%net_interface%" source=static addr=%Net_IP_Diy% mask=%net_MASK%
REM echo 设置IP成功...
REM Echo 设置网关...
REM netsh interface ip set address name="%net_interface%" gateway=%Net_gateway% gwmetric=1
REM echo 设置DNS...
REM netsh interface ip set dns name="%net_interface%" static addr=%Net_dnsPra% register=PRIMARY
REM netsh interface ip add dns name="%net_interface%" addr=%Net_dns2% index=2
wmic nicconfig where index=%NetConnectionID_Index% call enablestatic(%Net_IP_Diy%),(%net_MASK%)
wmic nicconfig where index=%NetConnectionID_Index% call setgateways(%Net_gateway%)
wmic nicconfig where index=%NetConnectionID_Index% call setdnsdomain(%Net_dnsPra%)
wmic nicconfig where index=%NetConnectionID_Index% call SetDNSServerSearchOrder(%Net_dns2%)
goto Set_IP_Done
:DHCP_ip
echo.
echo 正在设置IP为自动获取,请等待...
REM echo 设置IP中...
REM netsh interface ip set address name="%net_interface%" source=dhcp
REM echo 设置网关成功...设置DNS中...
REM netsh interface ip set dns "%net_interface%" source=dhcp
wmic path Win32_NetworkAdapterConfiguration.index=%NetConnectionID_Index% call enabledhcp
wmic path Win32_NetworkAdapterConfiguration.index=%NetConnectionID_Index% call SetDNSServerSearchOrder()
goto Set_IP_Done
:Diy_ip
set/p Net_IP_Diy=请输入IP地址 XXX.XXX.XXX.XXX:
set/p Net_gateway=请输入网关地址 XXX.XXX.XXX.XXX:
set/p Net_dnsPra=请输入主dns地址 XXX.XXX.XXX.XXX:
REM netsh interface ip set address name="%net_interface%"source=static addr=%Net_IP_Diy% mask=255.255.255.0
REM netsh interface ip set address name="%net_interface%" gateway=%Net_gateway% gwmetric=0
REM netsh interface ip set dns name="%net_interface%" source=static addr=%Net_dnsPra% register=PRIMARY
wmic nicconfig where index=%NetConnectionID_Index% call enablestatic(%Net_IP_Diy%),(%net_MASK%)
wmic nicconfig where index=%NetConnectionID_Index% call setgateways(%Net_gateway%)
wmic nicconfig where index=%NetConnectionID_Index% call setdnsdomain(%Net_dnsPra%)
wmic nicconfig where index=%NetConnectionID_Index% call SetDNSServerSearchOrder(%Net_dns2%)
goto Set_IP_Done
:Look_ip
Echo.
if "%LookNum%" == "2" (
Echo 打开:控制面板-网络连接...
ping -n 1 127.1>nul
start control ncpa.cpl
Echo 说明:开启环境延迟第二次Bat运行适配器属性不会改变,所以第二次次信息查看需要手动查看属性... &Echo.
echo,前次%net_interface%地址的IP:%IPAddress% &echo,前次%net_interface%子网掩码:%IPSubnet% &echo,前次%net_interface%默认网关:%DefaultIPGateway%
ping -n 6 127.1>nul &goto Set_IP_Done
)
set LookNum=1
set /a LookNum+=1
Echo 查看网络连接%net_interface%的属性:
set "w1=wmic nic where "NetConnectionID="%net_interface%"" get Index"
for /f %%a in ('%w1% ^| findstr /b [0-9]') do set "n=%%a"
set "w2=wmic nicconfig where "Index='%n%'" get DefaultIPGateway^,IPAddress^,IPSubnet"
for /f tokens^=1-2delims^={^" %%a in ('%w2% /value^|find "."')do set "%%a%%b"
echo,%net_interface%地址的IP:%IPAddress%
echo,%net_interface%子网掩码:%IPSubnet%
echo,%net_interface%默认网关:%DefaultIPGateway%
ping -n 6 127.1>nul
goto Set_IP_Done
:rebootnet
echo 重启网卡中...请稍等...
ping -n 3 127.1>nul
netsh interface set interface "%net_interface%" disabled
netsh interface set interface "%net_interface%" enable
goto Set_IP_Done
:Set_Moden_Web
Echo 需要修改%net_interface%IP为网关地址段[%Net_gateway%]...
set /p SetChoice_Web=是(1) 否(2) 返回(3) :
Echo.
IF NOT "%SetChoice_Web%"=="" SET SetChoice_Web=%SetChoice_Web:~0,1%
if /i "%SetChoice_Web%"=="1" goto Set_web_ip
if /i "%SetChoice_Web%"=="2" goto Set_no_web_ip
if /i "%SetChoice_Web%"=="2" goto DiyCHOICE
echo 输入的选择超出范围...
ping -n 3 127.1>nul
goto :Set_Moden_Web
:Set_web_ip
Rem 获得网关前三位,设置为IP前三位,如果不存在则创建
if "%Net_gateway%" == "" (echo 设置Net_gateway &set Net_gateway=192.168.1.1)
for /f "tokens=1-4 delims=/." %%a in ("%Net_gateway%") do (set Set_web_IPa_=%%a.%%b.%%c)
set /a Set_web_IPb_=%random%%%255+3
set "Set_web_net_IPnew=%Set_web_IPa_%.%Set_web_IPb_%"
echo IP前三位:%Set_web_IPa_% 后一位:%Set_web_IPb_%
echo 正在设置%net_interface%为网关IP[%Set_web_net_IPnew%]......
wmic nicconfig where index=%NetConnectionID_Index% call enablestatic(%Set_web_net_IPnew%),(%net_MASK%)
echo 已经设置%net_interface%为网关IP[%Set_web_net_IPnew%]......
goto :Set_no_web_ip
:Set_no_web_ip
Echo 当前网关为:%Net_gateway% ,正在打开 http://%Net_gateway% ....
ping %Net_gateway% >nul
start "%programfiles%\Internet Explorer\iexplore" http://%Net_gateway%
ping -n 3 127.1>nul
goto :Set_IP_Done
:Set_IP_Done
echo.
echo OK,设置完成...
ping -n 1 127.1>nul
echo OK,任务完成,返回选择菜单...
ping -n 1 127.1>nul
GOTO DiyCHOICE
:end
echo **********************退出......................*********************
Endlocal
ping -n 3 127.1>nul
exit
|