如何在设置命令以设置 IP 地址后在批处理文件中添加 IP 范围

如何在设置命令以设置 IP 地址后在批处理文件中添加 IP 范围

我尝试使用 set/p 命令设置静态 IP 地址,具体命令如下。但是如何在“高级”选项卡中添加允许我添加多个 IP 范围的 add 命令?

意思是我的静态 IP 是192.168.4.250 ,但我的 IP 范围是192.168.4.250 - 192.168.60.250

@echo off
echo ipv4 set address "Ethernet 3"
echo ipv4 netsh interface name="Ethernet 3"
set/p address=First Address:
set/p mask=First Mask:
set/p gate=First Gate:
netsh interface ipv4 set address name="Ethernet 3" static %address% %mask% %gate%

结果是

在此处输入图片描述

另外,IP 变量是第 3 个八位字节192.168.%A.255,但我还想将第 1 个和第 2 个八位字节更改为变量,如果我使用的命令是,我该怎么做%A IN (120,1,130)

答案1

我不是 100% 确定我是否理解了你的问题,
但我 100% 确定我的英语很弱/很差,抱歉......

无论如何,希望我能够帮到你...

@echo off && setlocal EnableDelayedExpansion

echo;iPv4 Set address "Ethernet 3" & echo;iPv4 netsh interface name="Ethernet 3"

:loop
cls & echo 

:: remove variable/value in using loop for command set "_var="
for %%i in ("address=", "mask=","gate=","dns1=","dns2=")do set "%%~i"

:: set variable/value in using loop for command to set /p "_var=Test to Screen" 
for %%i in ("address=First Address: ", "mask=First Mask: ","gate=First Gate: ","dns1=First DNS: ","dns2=Second DNS: "
     )do <con: set /p "_%%~i" || goto :loop

:: set variable/value in using loop for command set dns/ip 1st and 2nd
for %%i in (1.1.1.1,"1.0.0.1 index=2")do %__AppDir__%netsh.exe interface ipv4 add dns %%~i

:: set variable/value in using loop for command set "Ethernet 3" static ip/mask/gate
%__AppDir__%netsh.exe interface ipv4 set address name="Ethernet 3" static %_address% %_mask% %_gate%

rem For /L (1ST.IPV4.OCT.IP)do ...
for /L %%A in (198,1,200
    )do %__AppDir__%netsh.exe interface ipv4 set address name="Ethernet 3" static %%~A.128.60.255 %_mask% %_gate%

rem For /L (1ST.2ND.IPV4.OCT)do... and Set %%A.2ND.IPV4.OCT
for /L %%A in (198,1,200)do for /l %%B in (128,1,130
     )do %__AppDir__%netsh.exe interface ipv4 set address name="Ethernet 3" static %%~A.%%~B.60.255 %_mask% %_gate%
        
rem For /L (1ST.2ND.3RD.OCT)do ... and Set %%A.%%B.%%C.OCT
for /L %%A in (198,1,200)do for /l %%B in (128,1,130)do for /l %%C in (4,1,60
    )do %__AppDir__%netsh.exe interface ipv4 set address name="Ethernet 3" static %%~A.%%~B.%%~C.255 %_mask% %_gate% 

rem For /L (1ST.2ND.3RD.4TH)do ... and Set %%A.%%B.%%C.%%D
for /L %%A in (198,1,200)do for /l %%B in (128,1,130)do for /l %%C in (4,1,60)do for /l %%D in (253,1,254
     )do %__AppDir__%netsh.exe interface ipv4 set address name="Ethernet 3" static %%~A.%%~B.%%~C.%%~D %_mask% %_gate%

endlocal & goto :eof || exit /b 0 

  • 观察:我认为附加命令对于测试与新分配的 IP 的连接很有用,如果成功则退出循环:
@echo off && setlocal EnableDelayedExpansion

echo;iPv4 Set address "Ethernet 3" & echo;iPv4 Netsh Interface Name="Ethernet 3"

:loop
cls & echo; & for %%i in ("address=", "mask=","gate=","dns1=","dns2=")do set "%%~i"

for %%i in ("address=First Address: ", "mask=First Mask: ","gate=First Gate: ","dns1=First DNS: ","dns2=Second DNS: "
     )do <con: set /p "_%%~i" || goto :loop

for %%i in (1.1.1.1,"1.0.0.1 index=2")do %__AppDir__%netsh.exe interface ipv4 add dns %%~i
%__AppDir__%netsh.exe interface ipv4 set address name="Ethernet 3" static %_address% %_mask% %_gate%

for /L %%A in (198,1,200
    )do ;"%__AppDir__%netsh.exe" interface ipv4 set address name="Ethernet 3" static %%~A.128.60.255 %_mask% %_gate% && (
         <con: %__AppDir__%ping.exe 1.1.1.1 -f -4 -w 4|2>nul %__AppDir__%findstr.exe .*Received.=.[1-4].>nul && goto %:^)
        ) 

for /L %%A in (198,1,200)do for /l %%B in (128,1,130
     )do %__AppDir__%netsh.exe interface ipv4 set address name="Ethernet 3" static %%~A.%%~B.60.255 %_mask% %_gate% && (
         <con: %__AppDir__%ping.exe 1.1.1.1 -f -4 -w 4|2>nul %__AppDir__%findstr.exe "Received.=.[1-4]">nul && goto %:^)
        )
        
for /L %%A in (198,1,200)do for /l %%B in (128,1,130)do for /l %%C in (4,1,60
    )do %__AppDir__%netsh.exe interface ipv4 set address name="Ethernet 3" static %%~A.%%~B.%%~C.255 %_mask% %_gate% && (
         <con: %__AppDir__%ping.exe 1.1.1.1 -f -4 -w 4|2>nul %__AppDir__%findstr.exe "Received.=.[1-4]" >nul && goto %:^)
        )
        
for /L %%A in (198,1,200)do for /l %%B in (128,1,130)do for /l %%C in (4,1,60)do for /l %%D in (253,1,254
     )do ;%__AppDir__%netsh.exe interface ipv4 set address name="Ethernet 3" static %%A.%%B.%%C.%%D %_mask% %_gate% && (
         <con: %__AppDir__%ping.exe 1.1.1.1 -f -4 -w 4|2>nul %__AppDir__%findstr.exe "Received.=.[1-4]">nul && goto %:^)
        )
%:^) 
:: Your next command came here...
endlocal & goto :eof || exit /b 0

相关内容