Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Hostname to nslookup via batch file  (Read 7196 times)

0 Members and 2 Guests are viewing this topic.

tomsangels

    Topic Starter


    Newbie

    • Experience: Beginner
    • OS: Windows 10
    Hostname to nslookup via batch file
    « on: September 17, 2019, 03:26:32 AM »
    Hello,

    What i would like to achieve in a batch file:
    Hostname resolve to ip address and from the ip address -> nslookup the ip to resolve the hostname from a csv file (Hostname.csv).

    in csv file will contain only the hostname -> Hostname.csv
    and the result output tab should contain:
    Hostname | ip address | NSlookup hostname -> result.csv

    Thanks

    Hackoo



      Hopeful
    • Thanked: 42
    • Experience: Expert
    • OS: Windows 10
    Re: Hostname to nslookup via batch file
    « Reply #1 on: September 17, 2019, 06:29:46 AM »
    Hi  ;)
    Can you provide us an example of your input csv file ?
    How is the file constructed, that is, what is the delimiter ?
    So, your problem how to get the ip adress from the hostname ?
    « Last Edit: September 17, 2019, 06:56:24 AM by Hackoo »

    tomsangels

      Topic Starter


      Newbie

      • Experience: Beginner
      • OS: Windows 10
      Re: Hostname to nslookup via batch file
      « Reply #2 on: September 17, 2019, 04:47:55 PM »
      Hi,

      Below is somewhat im trying

      @echo off
      for /f %%i in (location of file\hostname.csv) do Call :StartPing %%i
      goto :eof

      :StartPing
      PING %1 -n 1| FIND /i "TTL" > nul && goto Success
      PING %1 -n 1| FIND /i "timed" > nul && goto Timedout
      PING %1 -n 1 -w 400 | FIND /i "TTL" > nul || goto ErrorMsg
      goto :EOF

      :Success
      for /F "tokens=3" %%a in ('ping %1 ^| find /i "TTL"') do set Address=%%a
      for /F "tokens=2" %%a in ('ping -a %Address::=% ^| find /i "pinging"') do set HostName=%%a

      set IPAddress=%Address::=%
      echo %1, %IPAddress%,%Hostname%

      for /f "tokens=2" %%a in ('nslookup %IPAddress% ^| find /i "Name: " ') do set "nsNAME=%%a"
      echo "%nsname%"

      echo %1, %IPAddress%,%Hostname% >> location of file Results.csv
      goto :EOF

      :Timedout
      Echo %1, Request timed out.
      Echo %1, Request timed out. >> location of file Results.csv
      goto :EOF

      :ErrorMsg
      Echo %1, Ping request could not find host.
      Echo %1, Ping request could not find host. >> Results.csv
      goto :EOF

      im stuck with nslookup export

      delimiter -> Hostname;ip adress;nslookup hostname (output should have this result altogther)

      hostname.csv -> are simply a list with hostname e.g computer 1
                                                                                   computer 2

      Thanks for the big help.
       
      « Last Edit: September 18, 2019, 07:32:29 AM by patio »