CMD Findstr用ping

时间:2013-03-18 19:03:32

标签: windows batch-file cmd ping findstr

我正在写一个ping网站的批处理文件,例如:

Pinging youtube.com [173.194.70.91] with 32 bytes of data:
Reply from 173.194.70.91: bytes=32 time=187ms TTL=44

Ping statistics for 173.194.70.91:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 187ms, Maximum = 187ms, Average = 187ms

我的程序必须只复制IP地址并将其保存到文本文件中。

所以它进入文本文件

ping youtube.com >> file.txt

现在我使用findstr命令查找IP地址。如果我使用它,那很好:

ping youtube.com -n 1 >nul >>C:\Users\Adrian\Desktop\IP.txt
findstr /i "Reply from 173.194.70.93: bytes=32 time=188ms TTL=43 C:\IP.txt

IF %ERRORLEVEL% EQU 0 (
  echo You are winning
) else (
  echo You are losing
)
pause

现在它说我赢了,这是一件好事,但我的程序必须在不知情的情况下找到IP地址。所以findstr命令必须知道在这里获取IP地址......

"Reply from 173.194.70.91: bytes=32 time=187ms TTL=44"

所以它必须findstr来自该字符串的IP。但我一直在谷歌搜索,一无所获。

2 个答案:

答案 0 :(得分:0)

试试这个:

@echo off &setlocal 
for /f "tokens=2delims=[]" %%i in ('ping -n 1 youtube.com^|find "["') do set "ip=%%i"
echo.%ip%>>file.txt
endlocal

答案 1 :(得分:0)

如果PowerShell可以接受,那么您可以尝试:

[System.Net.Dns]::GetHostAddresses("youtube.com") | foreach {echo $_.IPAddressToString }