Script to find host names from list of IPs

Discussion in 'Scripting' started by Azad, Mar 24, 2008.

  1. Azad

    Azad Guest

    I am looking for a script which can read a .txt file with a list of IP
    addreses and fetch me the hostnames. Ideally the output format should be "IP:
    xxx.xxx.xxx.xxx Host Name: ABCXXX". Could someone help me please?
    I may not have admin access on some of the boxes, the script I am looking
    for should work in these conditions as well. At the moment, I am using very
    rudimentary method of writing a batch file with several entries of Ping -a
    xxx.xxx.xxx.xxx >> output.txt which I have to massage it to get the desired
    result.
     
    Azad, Mar 24, 2008
    #1
    1. Advertisements

  2. You could do this with a simple batch file:
    @echo off
    for /F %%a in ('type d:\temp\IP.txt') do call :Sub %%a
    goto :eof

    :Sub
    for /F %%b in ('nbtstat -A %1 ^| find "<00>" ^| find /i "unique"') do echo
    %IP% Hostname: %%b
     
    Pegasus \(MVP\), Mar 24, 2008
    #2
    1. Advertisements

  3. Azad

    Azad Guest

    I get an error:

    'Hosname' is not recognized as an internal or external command,
    operable program or batch file.
     
    Azad, Mar 24, 2008
    #3
  4. Let's have a look at your batch file! Please mark clearly where
    your lines start, e.g. like so:
    [email protected] off
    Line2=for /F %%a in ('type d:\temp\IP.txt') do call :Sub %%a
    Line3=goto :eof
     
    Pegasus \(MVP\), Mar 24, 2008
    #4
  5. Azad

    Azad Guest

    Thank you! That did fetch some results.

    3 things that I noticed in the output:
    1. IP address column is not seen in the output.
    2. The Hostname shows <hostname>-W
    3. I had about 150 IPs in the IP.txt file, the output had only 43 hostnames
     
    Azad, Mar 25, 2008
    #5
  6. Sorry, unless you post your version of the code I cannot
    assist you.
     
    Pegasus \(MVP\), Mar 25, 2008
    #6
  7. As a further thought: You should also post the
    output you get when executing this command
    for an IP address that does NOT generate
    the expected output:

    nbtstat -A aaa.bbb.ccc.ddd
     
    Pegasus \(MVP\), Mar 25, 2008
    #7
  8. Azad

    Azad Guest

    ***Code***
    @echo off
    for /F %%a in ('type D:\test\IP.txt') do call :Sub %%a
    goto :eof

    :Sub
    for /F %%b in ('nbtstat -A %1 ^| find "<00>" ^| find /i "unique"') do echo
    %IP% Hostname %%b
    ***Command that I run***
    D:\Test>pinglist >> output.txt
    ***Output***
    Hostname 7996S1-W
    Hostname 7920S1-W
    ***Output of nbtstat -A of missing info***
    Local Area Connection 4:
    Node IpAddress: [172.17.224.83] Scope Id: []

    Host not found.
     
    Azad, Mar 26, 2008
    #8
  9. I made a mistake in my script. It should read like so:
    @echo off
    for /F %%a in ('type D:\test\IP.txt') do call :Sub %%a
    goto :eof

    :Sub
    for /F %%b in ('nbtstat -A %1 ^| find "<00>" ^| find /i "unique"') do echo
    %1 Hostname %%b

    About the command that fails to generate an output:
    nbtstat -A 10.9.160.40
    I cannot help you there. Either the machine 10.9.160.40
    has some firewall restriction or there is something wrong
    with its IP configuration. You can test the firewall part by
    executing the command nbtstat -A 10.9.160.40 on the
    machine itself.


     
    Pegasus \(MVP\), Mar 26, 2008
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.