WINDOWS PROMPT Getting IP of webpages from file

192 views Asked by At

I have to get IP of webpages that I'll deliver from txt file: for example txt file looks like:

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

I'll have to get something like

81.177.116.172
11.127.114.122
etc..
or 
81.177.116.172 - google.com
11.127.114.122 - yahoo.com

I know that I can use

ping websiteurl.com > ping.txt

but i want to check like 2000 pages. Please advice how to do it the fastest way, and how to deliver line from txt file as parameter to ping. Thanks

2

There are 2 answers

0
JosefZ On

Next updated and commented code snippet is faster than code in my original answer:

@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

Data:

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

Output (run from an open cmd window):

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

Beautified output:

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
Tony Hinkle On

I know this is a PowerShell answer to a batch question, but I find that many people ask how to do things in batch when they are really just wanting a command line. If you are wanting to learn how to do things like this, learn PowerShell instead of batch as it is much easier to learn than the convoluted batch FOR command (my apologies to those of you who actually understand it).

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