Sam*_*auk 6 ip-address ipconfig findstr cmd.exe windows-10
我试图让 cmd 只从命令输出中复制一组特定的字符。我已经能够选择该短语所在的行,但从那里我迷路了。到目前为止,这是我的命令。
ipconfig | findstr /i "ipv4"
Run Code Online (Sandbox Code Playgroud)
我相信你熟悉ipconfig
命令和findstr
命令。基本上我正在隔离特定计算机的 IPv4 地址。这就是上面的命令。
IPv4 Address. . . . . . . . . . . : 192.168.1.75
Run Code Online (Sandbox Code Playgroud)
我想隔离“192.168.1”。并将其保存到一个变量中,以便稍后在命令中使用。假设我设置了“192.168.1”。到变量a
。
我希望运行的下一个命令扫描从 192.168.1.1 到 192.168.1.254 的所有 IP 地址,并保存正在使用的地址。由于这个,即将成为程序,将在不同位置的不同计算机上运行“192.168.1”。会有所不同,我正在寻找隔离任何 cmd 放在它的位置。所以上述变量a可以是但不限于192.168.10.、10.10.10.,甚至1.1.1。视情况而定。这是我将使用的代码。
FOR /L %i IN (1,1,254) DO ping -n 1 (the 'a' variable)%i | FIND /i "bytes=">>c:\ipaddresses.txt
Run Code Online (Sandbox Code Playgroud)
或者如果查看变量的外观有帮助。
FOR /L %i IN (1,1,254) DO ping -n 1 192.168.1.%i | FIND /i "bytes=">>c:\ipaddresses.txt
Run Code Online (Sandbox Code Playgroud)
我使用的是 64 位 Windows 10 pro 机器。
使用以下批处理文件 (test.cmd):
@echo off
setlocal
setlocal enabledelayedexpansion
rem throw away everything except the IPv4 address line
for /f "usebackq tokens=*" %%a in (`ipconfig ^| findstr /i "ipv4"`) do (
rem we have for example "IPv4 Address. . . . . . . . . . . : 192.168.42.78"
rem split on : and get 2nd token
for /f delims^=^:^ tokens^=2 %%b in ('echo %%a') do (
rem we have " 192.168.42.78"
rem split on . and get 4 tokens (octets)
for /f "tokens=1-4 delims=." %%c in ("%%b") do (
set _o1=%%c
set _o2=%%d
set _o3=%%e
set _o4=%%f
rem strip leading space from first octet
set _3octet=!_o1:~1!.!_o2!.!_o3!.
echo !_3octet!
)
)
)
rem add additional commands here
endlocal
Run Code Online (Sandbox Code Playgroud)
笔记:
_3octet
echo
用于调试的rem add additional commands here
为您的“网络 ping”示例用法和输出:
F:\test>ipconfig | findstr /i "ipv4"
IPv4 Address. . . . . . . . . . . : 192.168.42.78
F:\test>test
192.168.42.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
39631 次 |
最近记录: |