![如何从成功的 ping 命令中提取 IP 地址?](https://linux22.com/image/1482853/%E5%A6%82%E4%BD%95%E4%BB%8E%E6%88%90%E5%8A%9F%E7%9A%84%20ping%20%E5%91%BD%E4%BB%A4%E4%B8%AD%E6%8F%90%E5%8F%96%20IP%20%E5%9C%B0%E5%9D%80%EF%BC%9F.png)
在另一位用户的大力帮助下,我能够从 CMD 的单个输出中截取字符。然后,我添加了他留给我的变量的下一部分代码_3octet
。我的代码正在扫描与计算机所连接的网关相关的所有 IP 地址,并仅捕获有回复的 IP 地址。从那里我得到了已回复的 IP 列表。
Reply from 192.168.1.67: bytes=32 time=288ms TTL=64
Reply from 192.168.1.72: bytes=32 time<1ms TTL=128
Reply from 192.168.1.73: bytes=32 time=16ms TTL=64
Reply from 192.168.1.75: bytes=32 time<1ms TTL=128
Reply from 192.168.1.76: bytes=32 time=155ms TTL=64
Reply from 192.168.1.88: bytes=32 time=1ms TTL=255
从那里我只想获取 IP 地址,因此删除除“192.168.1.#”之外的所有内容,以便在程序的后续部分使用。类似于代码开头部分中的变量_3octet
。这是我的代码。
@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!
)
)
)
cd C:\Windows
for /l %%i IN (1,1,254) do ping -n 1 !_3octet!%%i | for /f delims^=^:^ tokens^=1 %%g in (find /i "bytes=32") do (
for /f "tokens=1-4 delims=." %%j in ("%%g") do (
set _o1=%%j
set _o2=%%k
set _o3=%%l
set _o4=%%m
set _4octet=!_o1:~11!.!_o2!.!_o3!.!_o4!
echo !_4octet!
)
)
endlocal
我: was unexpected at this time.
多次收到输出。这告诉我它正在尝试 ping 1-254 中的每个地址,而不是作为回复出现的地址。不用说,我的代码没有像我希望的那样工作。
我计划在此代码中使用最终的 IP 地址列表:shutdown -r -m \\192.168.1.1 -t 10
以关闭连接到网关的计算机。
如果这让您感到困惑,请告诉我。谢谢。
答案1
我只想从成功的 ping 中获取 IP 地址
我不会尝试修复批处理文件的第二部分,从头开始编写它更容易。
使用以下批处理文件(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!
)
)
)
for /l %%i in (1,1,254) do (
rem do the ping and grab the 3rd token (ip address) if it succeeds (TTL= is found)
for /f "usebackq tokens=3" %%j in (`ping -n 1 !_3octet!%%i ^| find "TTL="`) do (
rem we have ip address with a trailing :
rem split on . and get the 4th token (last octet)
rem we don't need the first 3 as they are already in !_3octet!
for /f "tokens=4 delims=." %%k in ("%%j") do (
set _o4=%%k
rem strip trailing : from last octet
set _4octet=!_o4:~0,-1!
echo !_4octet!
)
)
)
endlocal
笔记:
您想要的值保存在
!_3octet!!_4octet!
哪里!_3octet! 与之前相同
!_4octet! 是最后一个八位字节(您不需要两次提取前 3 个八位字节!)
你不需要
cd C:\Windows
其中一个 IP 地址将是您的网关(路由器)地址,您可能不想尝试
shutdown
在网关上运行!