«  Friday Links #48 | Google Weather API report... »

Determining the local IP address via script

Why determining the local IP address should be such a monumental task in Windows is anyone’s guess. Here’s some JavaScript code I cobbled together from a couple of different sources for an installer script I’m writing.

function GetIpAddress()
{
    var ip = "";
    wmi = GetObject("winmgmts:!\\\\.\\root\\cimv2")
    nac = wmi.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")

    for (nac = new Enumerator(nac); !nac.atEnd(); nac.moveNext())
        ip += nac.item().ipAddress(0);

    if (ip == "")
        throw "IP address not found";
        
    return ip;
}

It works but isn't pretty. Surely there must be an easier way? (I’m not running in a browser so no “Remote_Server” header is available).

Signed: Irritated

 

Comments

RE: Determining the local IP address via script
by Joe Smith
Wednesday, April 29, 2009 9:51 AM

what happens when you have 5 ip interfaces?


powered by Bloget™