<http://www.netikka.net/tsneti/info/tscmd181.php>
Copyright © 2003-2010 by Prof. Timo Salmi  
Last modified Sat 6-Feb-2010 16:21:48
Welcoming the user from 54.82.79.109
On Sat 16-Dec-2017 13:07:51 local time

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



181} How do I get the IP-names for a logfile containing the IP-numbers?

Consider the following visits log file "myfile.log" (with no blank lines)
2010.02.01 04:33:55    24.31.147.42
2010.02.01 04:57:06    60.251.234.1
2010.02.01 07:13:00   38.100.41.120
2010.02.01 08:03:16 128.107.239.233
2010.02.01 08:05:49   77.253.23.205
2010.02.01 08:34:51    115.113.76.5
2010.02.01 08:49:30  203.82.252.252
2010.02.01 09:28:12  212.88.125.245

Based on that log we wish to produce the following output
  +----------------------------------------------------+
  | IPNAMES.CMD Get the names for a list of IP numbers |
  | By Prof. Timo Salmi, last modified Mon 1-Feb-2010  |
  +----------------------------------------------------+

  "myfile.log" 8 lines
  2010.02.01 04:33:55    24.31.147.42 cpe-24-31-147-42.maine.res.rr.com
  2010.02.01 04:57:06    60.251.234.1 60-251-234-1.HINET-IP.hinet.net
  2010.02.01 07:13:00   38.100.41.120 UnKnown
  2010.02.01 08:03:16 128.107.239.233 128-107-239-233.cisco.com
  2010.02.01 08:05:49   77.253.23.205 77-253-23-205.adsl.inetia.pl
  2010.02.01 08:34:51    115.113.76.5 115.113.76.5.static-hyderabad.vsnl.net.in
  2010.02.01 08:49:30  203.82.252.252 terminus1.tsl.net.hk
  2010.02.01 09:28:12  212.88.125.245 h1df5.n2.ips.mtn.co.ug

  @echo off & setlocal enableextensions enabledelayedexpansion
  ::
  :: Define location of a temporary folder and auxiliary files

  set temp_=%temp%
  if defined mytemp set temp_=%mytemp%
  set TempFileForOneNslookupLine=%temp_%\ipnames1line.tmp
  set TempFileForOutput=%temp_%\ipnames2.tmp
  set TailOfTheOriginal=%temp_%\ipnamesTail.tmp
  ::
  :: Ensure a clean plate

  for %%f in ("%TempFileForOneNslookupLine%"
    "%TempFileForOutput%"
    "%TailOfTheOriginal%") do if exist %%f del %%f
  ::
  :: Is help required?

  set help_=
  if "%~1"=="" set help_=true
  if "%~1"=="?" set help_=true
  if "%~1"=="/?" set help_=true
  if defined help_ (call :DisplayHelp & goto :EOF)
  ::
  :: Does the original log file exist?

  set OriginalLogFile=%~1
  if not exist "%OriginalLogFile%" (
    call :ProgTitle
    >con echo.
    >con echo File "%OriginalLogFile%" not found
    goto :EOF)
  ::
  :: Count the lines in the original log file

  call :CountLines "%OriginalLogFile%" lineCount
  for /f %%n in ("%lineCount%") do set lineCount=%%n
  ::
  :: Copy the entrire original log file or optionally get its tail

  if "%~2"=="" (
    copy "%OriginalLogFile%" "%TailOfTheOriginal%" > nul
    ) else (
    call :TypeTail "%OriginalLogFile%" %lineCount% %%2 %TailOfTheOriginal%)
  ::
  :: Do the resolving line by line

  for /f "tokens=1-3" %%a in ('type "%TailOfTheOriginal%"') do (
    call :FuncRightJustify15 %%c ipnum_
    nslookup %%c|find "Name: " > "%TempFileForOneNslookupLine%"
    call :GetOneIPname "%TempFileForOneNslookupLine%" ipname_
    echo %%a %%b !ipnum_! !ipname_!>> "%TempFileForOutput%")
  ::
  :: Present the outcome

  cls
  call :ProgTitle
  >con echo.
  if "%~2"=="" echo "%OriginalLogFile%" %lineCount% lines
  if not "%~2"=="" echo "%OriginalLogFile%" last %~2 of %lineCount% lines
  type "%TempFileForOutput%"
  ::
  :: Clean up

  :_cleanup
  for %%f in ("%TempFileForOneNslookupLine%"
    "%TempFileForOutput%"
    "%TailOfTheOriginal%") do if exist %%f del %%f
  endlocal & goto :EOF

  :ProgTitle
  >con echo +----------------------------------------------------+
  >con echo ^ IPNAMES.CMD Get the names for a list of IP numbers ^
  >con echo ^ By Prof. Timo Salmi, last modified Mon 1-Feb-2010  ^
  >con echo +----------------------------------------------------+
  endlocal & goto :EOF

  :DisplayHelp
  call :ProgTitle
  >con echo.
  >con echo Usage: IPNAMES [InputLogFileName] [OnlyNumberOfLinesFromTheEnd]
  endlocal & goto :EOF

  :CountLines
  rem Return the number of non-blank lines of file %1
  setlocal enableextensions
    set lineCount=
    for /f %%n in ('type %1') do set /a lineCount+=1
  endlocal & set %2=%lineCount% & goto :EOF

  :TypeTail
  rem Output to file %%4 the last %3 lines of file %1 which is of length %2
  setlocal enableextensions enabledelayedexpansion
    set lineCount=
    set /a x_=%2-%3
    for /f "tokens=*" %%a in ('type %1') do (
      set /a lineCount+=1
      if !lineCount! GTR %x_% echo %%a) >> %4
    goto :EOF

  :FuncRightJustify15
  setlocal enableextensions
    set str_=               %1
    set str_=%str_:~-15%
  endlocal & set "%2=%str_%" & goto :EOF

  :GetOneIPname
  setlocal enableextensions
    set return_=UnKnown
    for /f "tokens=2" %%a in ('type %1') do set return_=%%a
  endlocal & set "%2=%return_%" & goto :EOF
You might also wish to see the WHOISME.CMD script from tscmd.zip.