如何用批处理设置IP地址

来源:百度知道 编辑:UC知道 时间:2024/05/15 02:52:10
我想用批处理命令设置IP地址,运行该批处理可以切换IP,例如为:
(1)
IP地址:192.168.1.5
子网掩码:255.255.255.0
默认网关:192.168.1.1
首选DNS服务器:202.96.123.6
备用DNS服务器:201.99.224.8
(2)
IP地址:10.96.236.6
子网掩码:255.255.255.0
只有一个网卡。

@echo off
REM 选项:
:choice
echo 输入1,网卡IP将被设置成192.168.1.5,输入2,网卡IP将被设置成10.96.236.6
set /p select=请输入您的选择:
if not %select%==1 if not %select%==2 cls&echo 您的输入错误,请重新输入!&echo.&goto choice

REM 获取网卡名称
for /f "skip=4 tokens=3* delims= " %%i in ('ipconfig') do (
set adapter=%%i
goto out
)

:out
set adapter=%adapter::=%
if %select%==1 (
netsh int ip set address name="%adapter%" static 192.168.1.5 255.255.255.0 192.168.1.1 1
netsh int ip set dns name="%adapter%" static 202.96.123.6 primary
netsh int ip add dns name="%adapter%" 201.99.224.8 index=2
) else (
netsh int ip set address name="%adapter%" static 10.96.236.6 255.255.255.0 10.96.236.1 1
)

echo 设置完成,按任意键退出!
pause >nul 2>nul

用netsh命令编写

可以内外可以同时上。