以下是一段自己拼凑的批处理代码,可快速设置IP地址。
自动获取网络连接名称,多网络连接、多方案切换:
@echo off
set prgramname=IP地址快速设置
set producer=zyphio
set version=V1.2
title %prgramname% %producer% %version%
MODE con: COLS=80 lines=24
color 3a
:listnet
set netname=
@echo off&setlocal enabledelayedexpansion
set m=0
for /f "skip=3 tokens=4 delims= " %%a in ('netsh interface show interface') do (
set /a m+=1
set name!m!=%%a
)
cls
echo %prgramname%
echo %producer%
echo %version%
echo.
echo 检测到的网络连接如下:
echo.
echo 1. !name1!
echo.
echo 2. !name2!
echo.
echo 3. !name3!
echo.
echo 4. !name4!
echo.
echo 5. !name5!
echo.
echo 6. !name6!
echo.
echo 7. !name7!
echo.
echo 8. !name8!
echo.
CHOICE /C 12345678RVE /N /M "选择要配置的网络请按[1-8],刷新列表按[R],查看IP配置请按[V],退出按[E]:"
if errorlevel 11 goto end
if errorlevel 10 goto listip
if errorlevel 9 goto listnet
if errorlevel 8 set netname=!name8!
if errorlevel 7 set netname=!name7!
if errorlevel 6 set netname=!name6!
if errorlevel 5 set netname=!name5!
if errorlevel 4 set netname=!name4!
if errorlevel 3 set netname=!name3!
if errorlevel 2 set netname=!name2!
if errorlevel 1 set netname=!name1!
goto planning
:planning
cls
echo.
echo 为当前选定 "!netname!" 设置IP地址:
echo.
echo 1.自动获取IP地址
echo.
echo 2.指定IP地址(192.168.1.56)
echo.
echo 3.指定IP地址(192.168.1.151)
echo.
echo 4.指定IP地址(192.168.0.151)
echo.
echo 5.指定IP地址(192.168.123.151)
echo.
echo 注意:请避免IP冲突,不同网络连接的IP地址不能相同!
echo.
choice /C 12345BE /N /M "选择方案请按[1-5],返回请按[B],退出按[E]:"
if errorlevel 7 goto end
if errorlevel 6 goto listnet
if errorlevel 5 goto solution5
if errorlevel 4 goto solution4
if errorlevel 3 goto solution3
if errorlevel 2 goto solution2
if errorlevel 1 goto solution1
:solution1
cls
echo.
echo 把 "!netname!" 设置为自动获取IP地址....
echo.
echo 自动获取IP地址....
netsh interface ip set address name = "!netname!" source = dhcp >nul
echo.
echo 自动获取DNS服务器....
netsh interface ip set dns name = "!netname!" source = dhcp >nul
sc config WinHttpAutoProxySvc start= demand >nul
sc stop WinHttpAutoProxySvc >nul
sc config Dhcp start= auto >nul
sc stop Dhcp >nul
sc start WinHttpAutoProxySvc >nul
sc start Dhcp >nul
echo.
goto complete
:solution2
set staddr=192.168.1.56
set stmask=255.255.255.0
set stgway=192.168.1.1
set stdns1=119.29.29.29
set stdns2=1.1.1.1
goto setip
:solution3
set staddr=192.168.1.151
set stmask=255.255.255.0
set stgway=192.168.1.1
set stdns1=119.29.29.29
set stdns2=1.1.1.1
goto setip
:solution4
set staddr=192.168.0.151
set stmask=255.255.255.0
set stgway=192.168.0.1
set stdns1=119.29.29.29
set stdns2=1.1.1.1
goto setip
:solution5
set staddr=192.168.123.151
set stmask=255.255.255.0
set stgway=192.168.123.1
set stdns1=119.29.29.29
set stdns2=1.1.1.1
goto setip
:setip
cls
echo.
echo 为 "!netname!" 设置IP地址...
echo.
echo IP地址:%staddr%
echo.
echo 子网掩码:%stmask%
echo.
echo 网关:%stgway%
netsh interface ip set address "!netname!" static %staddr% %stmask% %stgway% 1 >nul
echo.
echo 首选DNS:%stdns1%
netsh interface ip set dns name="!netname!" static %stdns1% primary >nul
echo.
echo 备用DNS:%stdns2%
netsh interface ip add dns name="!netname!" %stdns2% 2 >nul
echo.
choice /t 1 /d y /n >nul
goto complete
:complete
cls
echo.
ipconfig /flushdns >nul
choice /C BVE /N /M "操作完成,返回请按[B],查看IP配置请按[V],退出请按[E]:"
if errorlevel 3 goto end
if errorlevel 2 goto listip
if errorlevel 1 goto listnet
:listip
cls
choice /t 1 /d y /n >nul
ipconfig
echo.
choice /C BRE /N /M "返回请按[B],刷新请按[R],退出请按[E]:"
if errorlevel 3 goto end
if errorlevel 2 goto listip
if errorlevel 1 goto listnet
:end
cls
echo.&echo 按任意键退出...
pause>nul