WINDOWS PROMPT从文件中获取网页的IP

时间:2015-06-15 18:34:28

标签: batch-file ip ping prompt

我必须获取我将从txt文件提供的网页IP: 例如txt文件看起来像:

google.com
yahoo.com
toyota.com
bmw.com
etc...

我必须得到像

这样的东西
81.177.116.172
11.127.114.122
etc..
or 
81.177.116.172 - google.com
11.127.114.122 - yahoo.com

我知道我可以使用

ping websiteurl.com > ping.txt

但我想检查2000页。请以最快的方式建议如何实现,以及如何将txt文件中的行作为参数传递给ping。感谢

2 个答案:

答案 0 :(得分:0)

下一步更新评论代码段比原始答案中的代码更快:

@ECHO OFF
SETLOCAL enableextensions disabledelayedexpansion

set "_format=%~1"                   use for output ''beautifying''
if defined _format (
  echo        hostname OP IPv4_address    explanation
  echo        -------- -- ------------    -----------
)
set "_file=files\30852528.txt"      change to fit your circumstances

for /F "usebackq delims=" %%i in ("%_file%") do (
  set "_host=%%i"                   remember input line 

  for /F "tokens=1 delims==" %%G in ('set ___ping 2^>NUL') do set "%%G="

  set /A "_line=0" 
  for /F "delims=" %%G in ('ping -a -4 -n 1 %%i') do (
    set /A "_line+=1"
    call set "___ping%%_line%%=%%G" remember ping output to an array-like variable
  )
  REM   rem debug output in next two lines
  REM   echo(
  REM   for /F "tokens=1* delims==" %%G in ('set ___ping 2^>NUL') do echo(%%H
  call :pings
) 
ENDLOCAL
goto :eof

:pings
  set "_operator=??"
  set "_hostIPno=%_host%"
  set "_hostName=%_host%"
  set "_auxiliar="
  set "_explains=Unknown reason"    default values set

  set "_string=Ping request could not find host"
  for /F "delims=" %%G in ('set ___ping^|findstr /i /C:"%_string%"') do (
      set "_operator=##"
      set "_explains=%_string%"     ping request could not find host
      goto :pingsDone
  )

  set "_string==Pinging"
  for /F "tokens=1-4 delims==[] " %%G in ('set ___ping^|findstr /i /C:"%_string%"') do (
      echo("%%J"|findstr /r "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*">NUL
      if errorlevel 1 (
          set "_auxiliar=%%H %%I"
      ) else (
          set "_auxiliar=%%H %%I [%%J]"
          set "_hostIPno=%%J"
          set "_hostName=%%I"       address to hostname resolved
      )
  )

  set "_string=Destination host unreachable"
  for /F "tokens=1* delims==" %%G in ('set ___ping^|findstr /i /C:"%_string%"') do (
      set "_explains=%%H"
      set "_operator=?="            destination host unreachable
  )

  set "_string=Request timed out"
  for /F "tokens=1* delims==" %%G in ('set ___ping^|findstr /i /C:"%_string%"') do (
      set "_explains=%_auxiliar%: %%H"
      set "_operator==?"            request timed out
  )

  set "_string=TTL="
  for /F "tokens=1* delims==" %%G in ('set ___ping^|findstr /i "%_string%"') do (
      set "_explains=%%H"
      set "_operator==="            ping request successful
  )
:pingsDone
  rem basic formatting: output to columns only for demonstration at StackOverflow
  if defined _format (
      set "_hostName=               %_hostName%"
      set "_hostIPno=%_hostIPno%               "
      set "_explains= %_explains%"
  ) else set "_explains="
  if defined _format (
      set "_hostName=%_hostName:~-15%"
      set "_hostIPno=%_hostIPno:~0,15%"
  )

  rem output with delayed expansion enabled:
  SETLOCAL enabledelayedexpansion
    echo(!_hostName! !_operator! !_hostIPno!!_explains!
  ENDLOCAL
goto :eof

数据

d:\bat> type "files\30852528.txt"|findstr /V "^;"
foo.bar
google.com
77.75.79.53
192.168.1.1
192.168.1.12
bmw.com
160.46.244.131

输出(从打开的cmd窗口运行):

d:\bat> so\30852528.bat
foo.bar ## foo.bar
google.com == 216.58.209.206
www.seznam.cz == 77.75.79.53
192.168.1.1 == 192.168.1.1
192.168.1.12 ?= 192.168.1.12
bmw.com =? 160.46.244.131
origin.bmw.com =? 160.46.244.131

美化输出

d:\bat> so\30852528.bat 1
       hostname OP IPv4_address    explanation
       -------- -- ------------    -----------
        foo.bar ## foo.bar         Ping request could not find host
     google.com == 216.58.209.206  Reply from 216.58.209.206: bytes=32 time=24ms TTL=56
  www.seznam.cz == 77.75.79.53     Reply from 77.75.79.53: bytes=32 time=10ms TTL=248
    192.168.1.1 == 192.168.1.1     Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
   192.168.1.12 ?= 192.168.1.12    Reply from 192.168.1.100: Destination host unreachable.
        bmw.com =? 160.46.244.131  Pinging bmw.com [160.46.244.131]: Request timed out.
 origin.bmw.com =? 160.46.244.131  Pinging origin.bmw.com [160.46.244.131]: Request timed out.

答案 1 :(得分:0)

我知道这是批量问题的PowerShell答案,但我发现许多人在他们真正想要命令行时会问如何批量处理。如果您想学习如何做这样的事情,请学习PowerShell而不是批处理,因为它比复杂的批处理命令更容易学习(我向那些真正了解它的人道歉)。

get-content c:\temp\test.txt | foreach-object{ping ([System.Net.Dns]::GetHostAddresses("$_")[0].IPAddressToString)}