1、手动IP配置
①单网卡时,无需选择网卡
②需要管理员权限
③可以根据不同的需要修改脚本实现不同的功能
@echo off
::管理员身份
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
::查询所有网卡名称
for /f "tokens=4,5 delims= " %%i in ('netsh interface show interface') do (
echo %%i %%j
if "%%j" == "%kong%" (
set int_temp=%%i
) else (
set int_temp=%%i %%j
)
set /a num1+=1
)
echo.
echo.
::判断是否只有一直张网卡
if %num1% == 2 (
set network_name=%int_temp%
) else (
set /p network_name=请输入网卡名称:
)
echo.
echo.
set kong=
::查找选择的网卡
for /f "tokens=4,5 delims= " %%i in ('netsh interface show interface ^| find "%network_name%"') do (
::判断网卡名称中间是否存在空格
if "%%j" == "%kong%" (
set interface=%%i
) else (
set interface=%%i %%j
)
)
echo ----------%interface%----------
echo.
echo.
if "%interface%" == "%kong%" (
echo 未找到该网卡
pause
exit
)
::DHCP配置
::netsh interface ipv4 set address %interface% dhcp
::netsh interface ipv4 set dns %interface% validate=no dhcp
set /p ip=ip地址:
set mask=255.255.255.0
set /p gat=网关:
set dns1=192.168.1.1
set dns2=223.5.5.5
netsh interface ipv4 set address "%interface%" static %ip% %mask% %gat%
netsh interface ipv4 set dns "%interface%" validate=no static %dns1%
netsh interface ipv4 add dns "%interface%" validate=no %dns2%
echo IP配置完成
echo.
echo.
pause
2、全自动IP配置
①自动选择已连接的网卡
②前提网卡要开启并连接状态
@echo off
::管理员身份
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
set kong=
::查找选择的网卡
for /f "tokens=4,5 delims= " %%i in ('netsh interface show interface ^| find "已连接"') do (
::判断网卡名称中间是否存在空格
if "%%j" == "%kong%" (
set interface=%%i
) else (
set interface=%%i %%j
)
)
echo ----------%interface%----------
echo.
echo.
if "%interface%" == "%kong%" (
echo 未找到网卡,请确保网卡已开启并连接
pause
exit
)
netsh interface ipv4 set address "%interface%" static 192.168.1.154 255.255.255.0 192.168.1.1
netsh interface ipv4 set dns "%interface%" validate=no static 192.168.1.1
netsh interface ipv4 add dns "%interface%" validate=no 223.5.5.5
echo IP配置完成
echo.
echo.
pause