求大佬帮写一个添加IP的批处理或脚本

比如我的电脑IP是192.168.2.*,在获取到这个IP后,最后一位“*”不变的情况下添加一个192.168.20段的IP,如下图

不清楚你的实际文件/情况,仅以问题中的样例/说明为据;以下代码复制粘贴到记事本,另存为xx.bat,编码选ANSI
@echo off
rem 添加一个额外网段的ip但不改变/保留最后一段的ip数字
set #=Any question&set @=WX&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$%/%@% %z%
set "NetConnectionID="
set "MACAddress="
set "IPAddress="
for /f "delims=" %%a in ('wmic nic where "NetEnabled=TRUE and PhysicalAdapter=TRUE" get NetConnectionID^,MACAddress /value^|find "="') do set %%a
if not defined NetConnectionID (echo;Failed to get NetConnectionID&pause&exit)
for /f "delims=" %%a in ('wmic nicconfig where "MACAddress='%MACAddress%'" get IPAddress^,DHCPEnabled /value^|find "="') do set %%a
if not defined IPAddress (echo;Failed to get IPAddress&pause&exit)
if /i "%DHCPEnabled%" equ "TRUE" (echo;DHCP is Enabled&pause&exit)
set IPAddress=%IPAddress:{=}%
set IPAddress=%IPAddress:}=%
set IPAddress=%IPAddress:"=%
for /f "delims=," %%a in ("%IPAddress%") do (
set oldip=%%a
for /f "tokens=1-4 delims=." %%b in ("%%a") do (
set #1=%%b
set #2=%%c
set #3=%%d
set #4=%%e
)
)
set newip=192.168.20.%#4%
set newipsubnet=255.255.255.0
echo;netconnection=%NetConnectionID%
echo;oldip=%oldip%
echo;mac=%MACAddress%
echo;--------------------------
echo;newip=%newip%
echo;newipsubnet=%newipsubnet%
netsh interface ip add address "%NetConnectionID%" %newip% %newipsubnet%
echo;%#% +%$%%$%/%@% %z%
pause
exit
温馨提示:答案为网友推荐,仅供参考
相似回答