List of servers, get IP addresses?

I have a large list of servers, which I need IP addresses for. Does anyone know of any easy way (freeware, script, etc) that I can match a list of servers to IP addresses (I can do it manually with cut/paste and ping, but that’s slow and tedious, and removes time that could be spent reading the SDMB!)

Thanks,

-Butler

I suppose my answer depends on the OSes of the servers. There are utilities that will scan a subnet and return information (including name and IP) for all devices on the network that are SNMP compliant or are running WMI (Win2K, XP 2K3).

Most are demo copies that I’ve had good luck with.

This is the Network section of download.com which has many network inventory tools.

Many will work with *nix and Mac OSes too.

Hmm… didn’t think of download.com

I don’t need any SNMP queries, just a return from a command prompt type ping.

I’ll check them out, so that I can do less cut/paste, and more of something else!

Thanks,

-Butler (Who should know this stuff already!)

Here, I wrote you a script. Save it as “lottalookups.js”, edit the list of hostnames at the top, and execute it from a command prompt like this: “cscript //nologo lottalookups.js”.

This could easily be modified to read the list of hostnames from a text file instead of having them hardcoded in the script.



var fs = null;
var shell = null;

var gHostnames = [
    "www.google.com",
    "www.yahoo.com",
    "boards.straightdope.com",
    "www.ebay.com"
    ];
function Output(str)
{
    WScript.Echo(str);
}

function Init()
{
    shell = WScript.CreateObject("WScript.Shell");
    if(!shell)
    {
        Output("error creating WScript.Shell, bailing.");
        return 1;
    }

    fs = WScript.CreateObject("Scripting.FileSystemObject");
    if(!fs)
    {
        Output("error creating Scripting.FileSystemObject, bailing.");
        return 1;
    }

    return 0;
}

// execs cmd and returns output string
function shellexec(cmd, output)
{
    var result = "";
    var eTempFolder = 2;
    var eForReading = 1;
    var tempfolder = fs.GetSpecialFolder(eTempFolder);
    var tempfolderpath = tempfolder.Path;
    var tempfilename = fs.GetTempName();

    var tempfilepath = tempfolderpath + "\\" + tempfilename;

    var retval = shell.Run("cmd /c " + cmd + " > \"" + tempfilepath + "\"", 0, true);

    var tempfile = fs.OpenTextFile(tempfilepath, eForReading, false);

    if(tempfile)
    {
        try
        {
            result = tempfile.ReadAll();
        }
        catch(e)
        {
            result = "";
        }
        tempfile.Close();
        fs.DeleteFile(tempfilepath, true);
    }
    else
    {
        Output("shellexec failed to open tempfile");
    }
    if(output)
    {
        output.text = result;
    }

    return retval;
}

function main()
{
    var result = 0;

    // initialize global script runtime objects
    if(result = Init())
        return result;
    var out = new Object();

    for(var i=0; i<gHostnames.length; i++)
    {
        var bSuccess = false;
        var cmd = "ping -n 1 " + gHostnames* + " -w 0";
        result = shellexec(cmd, out);
        
        var lines = out.text.split("
");
        for(var j=0; j<lines.length; j++)
        {
            var pattern = /Pinging [^ ]* \[([^\]]*)\]/;
            if(pattern.exec(lines[j]))
            {
                Output(gHostnames* + ": " + RegExp.$1);
                bSuccess = true;
                break;
            }
        }

        if(!bSuccess)
        {
                Output(gHostnames* + ": not found.");
        }
    }
}

main();


The script does a “ping -n 1 -w 0” on each host listed and then parses the output for a line that says “Pinging xxxxx [n.n.n.n]”. It takes a long time on a failed name lookup, though, so this probably isn’t the best approach if you’re testing lots of names that might be bogus. I tried using nslookup at first, but couldn’t figure out how to get satisfactory output from it.

Here’s another option:

http://www.jimprice.com/jim-soft.htm#nsbatch

I assume you’re not running Unix, or else you wouldn’t have asked the question: A Unix-head would have coded up a shell script on the prompt and gotten it done already. :wink:



#!/bin/sh
# A script for any sh-like shell (bash, ksh, zsh)

NAMES=namelist
ADDYS=iplist

for i in `cat $NAMES`
do
host $i | awk '{print $1, $4}'
done > $ADDYS


host, above, determines the IP address given the machine name, and awk prints the first and fourth fields of every line host returns. This script, when run, takes a file with a list of hostnames (alter the value of NAMES to change which file is used) and creates a file with a list of hostname-IP address pairs (alter the value of ADDYS to change which file gets written to). There is, in both cases, one record per line, although host may return multiple IP addresses for the same hostname.

It should be trivial to change the above script to do what you want, or massage the data in the output file to be more to your liking. I can elaborate if you don’t understand something.

If you need a good shell for your (Windows, presumably) computer, check out Cygwin.

Folks are making this harder than it needs to be …

One line of Windows CMD will get you 85% of the way there & you can use Word to finish the job.
Create your list of servers as a text file, one name per line, something like:


www.sdmb.com
www.google.com
www.straightdope.com
www.badDNSname.wer
www.irs.gov

Save that file someplace named “ServerList.txt”, open a CMD window & CD to the folder where the file is. Enter these 2 commands:


C:\...>(for /f %e in (ServerList.txt) do echo Server: %e ## & tracert -h 1 %e)  | findstr "^Server ^Tracing ^Unable" > ServerAndIPAddressList.txt
C:\...>ServerAndIPAddressList.txt

Viola! Notepad opens with a file that looks like this:


Server: www.sdmb.com ##
Tracing route to www.sdmb.com [69.25.27.173]
Server: www.google.com ##
Tracing route to www.google.akadns.net [64.233.167.104]
Server: www.straightdope.com ##
Tracing route to straightdope.com [69.20.125.245]
Server: www.badDNSname.qwe ##
Unable to resolve target system name www.badDNSname.qwe.
Server: www.irs.gov ##
Tracing route to a321.g.akamai.net [66.77.99.136]

So now you have the servername, the real name if the name you used was an alias (which is usually the case), and either the IP address or an error message. That’s plenty good enough for people to read, but if you need something more strongly formatted to feed into another program, you can then open that ServerAndIPAddressList.txt file in Word, and do the following 5 Replace Alls (without the quotes)


Find: "Server: " -> Replace: ""
" ##^p" -> ","
"Tracing route to " -> ""
" [" -> ","
"]" -> ""

The result of that looks like this:


www.sdmb.com,www.sdmb.com,69.25.27.173
www.google.com,www.google.akadns.net,64.233.167.104
www.straightdope.com,straightdope.com,69.20.125.245
www.badDNSname.qwe,Unable to resolve target system name www.badDNSname.qwe.
www.irs.gov,a321.g.akamai.net,69.44.123.142

If you save that result from Word as a txt file named “AllNames.txt”, you can then separate the good & bad DNS names with these 2 CMD commands:


C:\...>find /v "Unable to" <AllNames.txt >GoodNames.txt
C:\...>find "Unable to" <AllNames.txt >BadNames.txt

GoodNames.txt is now ready to feed into a database or spreadsheet or ???, and BadNames.txt can be checked by the people to figure out where the bad names came from & how to fix them.

That wasn’t so hard.

This is a little freeware Windows utility that help. Specify an IP address range and it will return (among other things) the hostnames of all the systems in that range.

Ahh yes, but he had a list of names and wanted their IP addresses.

Yes, but that utility will work if all of the servers are on the same subnet.

Let’s see… 3 seperate programs and 5 manual search/replace operations. And you think a push-button script is “harder than it needs to be”? The OP said he was trying to avoid tedium, not create it.

:wink:

nmap. It’s opensource, and if you have a linux box, it’s probably already installed. Plus, everybody should know his nmap.

nmap -sP <address range>

Galt,

Exactly what I’m looking for, what would I change to get it from a text file? putting in quotes around my list would be nearly as tedious as the cut and paste ahead of me!

I don’t imagine that there will be very many entries that don’t have DNS, so the delays aren’t a big deal for me.

THANK YOU!!! :smiley:

-Butler
(yes everyone else, I know linux will solve all the worlds problems, bring true enlightenment, and generally make me 1337, but this is a M$ environment, and that’s where I’m putting my efforts! Though, in concept, I agree on all of those.)

<background> This particular work location is very tough for me, due to the strict procedures in place, it’s a HUGE operation. My group handles 5000 servers. I come from the 1-10 server environment, so the “tools” I’ve used there, aren’t the same as I’d use here… they wouldn’t scale. And it’s a wide network, with locations all over the world… what a learning curve! I’m used to a more “local” environment, and knowing everyone in the company! (4500 employees at this site alone!) What a change. </background>

Ok, here’s the script written to read the input list from a text file. Run it like this:
cscript //nologo lottalookups hosts.txt

I also added an “OutputHost” function so it’s easier for you to modify to spit out the format you like. One thing this lets you do is to have it put a tab in between the hostname and the IP instead of ": ", so you could redirect the output to a text file and then just open it in excel as a table. Just go:
cscript //nologo lottalookups hosts.txt > hostips.txt



var fs = null;
var shell = null;

function Output(str)
{
    WScript.Echo(str);
}

function Init()
{
    shell = WScript.CreateObject("WScript.Shell");
    if(!shell)
    {
        Output("error creating WScript.Shell, bailing.");
        return 1;
    }

    fs = WScript.CreateObject("Scripting.FileSystemObject");
    if(!fs)
    {
        Output("error creating Scripting.FileSystemObject, bailing.");
        return 1;
    }

    return 0;
}

// execs cmd and returns output string
function shellexec(cmd, output)
{
    var result = "";
    var eTempFolder = 2;
    var eForReading = 1;
    var tempfolder = fs.GetSpecialFolder(eTempFolder);
    var tempfolderpath = tempfolder.Path;
    var tempfilename = fs.GetTempName();

    var tempfilepath = tempfolderpath + "\\" + tempfilename;

    var retval = shell.Run("cmd /c " + cmd + " > \"" + tempfilepath + "\"", 0, true);

    var tempfile = fs.OpenTextFile(tempfilepath, eForReading, false);

    if(tempfile)
    {
        try
        {
            result = tempfile.ReadAll();
        }
        catch(e)
        {
            result = "";
        }
        tempfile.Close();
        fs.DeleteFile(tempfilepath, true);
    }
    else
    {
        Output("shellexec failed to open tempfile");
    }
    if(output)
    {
        output.text = result;
    }

    return retval;
}

function OutputHost(hostname, address)
{
    Output(hostname + ": " + address);
}

function main()
{
    var result = 0;
    var inputname = null;
    
    // initialize global script runtime objects
    if(result = Init())
        return result;

    try
    {
        inputname = WScript.Arguments(0);
    }
    catch(e)
    {
    }

    if(!inputname || inputname.length <= 0)
    {
        Output("please specify a text file containing a list of servers, one per line.");
        return 1;
    }

    var inputfile = null;
    
    try
    {
        inputfile = fs.OpenTextFile(inputname, 1, false);
    }
    catch(e)
    {
    }
    
    if(!inputfile)
    {
        Output("Error: couldn't open \"" + inputname + "\"");
        return 1;
    }

    var out = new Object();

    while(!inputfile.AtEndOfStream)
    {
        var hostname = inputfile.ReadLine();
        
        var bSuccess = false;
        var cmd = "ping -n 1 " + hostname + " -w 0";
        result = shellexec(cmd, out);
        
        var lines = out.text.split("
");
        for(var j=0; j<lines.length; j++)
        {
            var pattern = /Pinging [^ ]* \[([^\]]*)\]/;
            if(pattern.exec(lines[j]))
            {
                OutputHost(hostname, RegExp.$1);
                bSuccess = true;
                break;
            }
        }

        if(!bSuccess)
        {
            OutputHost(hostname, "not found");
        }
    }
}

main();


And for what it’s worth, I like derleth’s solution the best. I’m a unix nerd at heart (I got yer true enlightenment right here), but if you’re in a windows environment, it’s easier (and more powerful) to learn a little about the Windows Script Host than it is to fight it and use something like cygwin.

Thanks, I’ll give it a shot tommorow. this is just what I’m looking for. I knew some geek (fellow geek, more skilled though!) would know!!! :smiley: :smiley:

-Butler

Why reinvent the wheel?

For something this simple, if I can implement it myself and avoid running untrusted executables, I will. I mean heck, I wrote the script in less time than it would take me to cull through the google search results.

That, and the fact that I didn’t know nsbatch existed when I wrote the script. :slight_smile: But you’re right, it does exactly what he’s asking for, plus a whole lot more.