Get IP Address & FQDN

M. Gallant 08/16/2002

Currently, WSH v5.6 does not allow direct access to the local (or remote) machine IP network address. IP address information is, however, available using the WMI Win32_NetworkAdapterConfiguration class. However, earlier versions of the Win32 OS will not have WMI support. Also, in some cases, a client script may wish to know the FQDN (Fully Qualified Domain Name) given the IP address of the local machine, which requires quering a DNS server directly. This capability is available in script via the DNS WMI Provider, but the provider must be installed on the DNS server being queried. To provide scripting capability which works on Win95 - Win2000, the WSH VBS script below uses win32 native utilities to extract the local machine IP addresses and resolve to Fully Qualified Domain Names (FQDN) based on the default DNS server currently configured.

getipname.vbs

'**************************************************************** ' File: getipname.vbs (WSH for VBscript) ' Author: M. Gallant 09/30/2001 ' ' Based on script by M. Harris & T. Lavedas: ' posted to: microsoft.public.scripting.vbscript 2000/07/21 ' Reads IP addresses via: ' ipconfig.exe (NT4 and Win2000) ' winipcfg.exe (Win95) ' For NT4, Win2000 resolves IP addresses to FQDN names via: ' nslookup.exe (with default DNS server) ' '**************************************************************** arAddresses = GetIPAddresses() WScript.echo ubound(arAddresses)+1 & " IP Address(es) found" for each ip in arAddresses info = ip & vbTab & GetFQDN(ip) WScript.echo info next Function GetFQDN(ipaddress) '==== ' Returns Fully Qualified Domain Name ' from reverse DNS lookup via nslookup.exe ' only implemented for NT4, 2000 '==== set sh = createobject("wscript.shell") set fso = createobject("scripting.filesystemobject") Set Env = sh.Environment("PROCESS") if Env("OS") = "Windows_NT" then workfile = fso.gettempname sh.run "%comspec% /c nslookup " & ipaddress & " > " & workfile,0,true set sh = nothing set ts = fso.opentextfile(workfile) data = split(ts.readall,vbcr) ts.close set ts = nothing fso.deletefile workfile set fso = nothing for n = 0 to ubound(data) if instr(data(n),"Name") then parts = split(data(n),":") hostname= trim(cstr(parts(1))) Exit For end if hostname = "could not resolve IP address" next GetFQDN = hostname else set sh = nothing set fso = nothing GetFQDN = "" end if End Function Function GetIPAddresses() '===== ' Returns array of IP Addresses as output ' by ipconfig or winipcfg... ' ' Win98/WinNT have ipconfig (Win95 doesn't) ' Win98/Win95 have winipcfg (WinNt doesn't) ' ' Note: The PPP Adapter (Dial Up Adapter) is ' excluded if not connected (IP address will be 0.0.0.0) ' and included if it is connected. '===== set sh = createobject("wscript.shell") set fso = createobject("scripting.filesystemobject") Set Env = sh.Environment("PROCESS") if Env("OS") = "Windows_NT" then workfile = fso.gettempname sh.run "%comspec% /c ipconfig > " & workfile,0,true else 'winipcfg in batch mode sends output to 'filename winipcfg.out workfile = "winipcfg.out" sh.run "winipcfg /batch" ,0,true end if set sh = nothing set ts = fso.opentextfile(workfile) data = split(ts.readall,vbcr) ts.close set ts = nothing fso.deletefile workfile set fso = nothing arIPAddress = array() index = -1 for n = 0 to ubound(data) if instr(data(n),"IP Address") then parts = split(data(n),":") if trim(parts(1)) <> "0.0.0.0" then index = index + 1 ReDim Preserve arIPAddress(index) arIPAddress(index)= trim(cstr(parts(1))) end if end if next GetIPAddresses = arIPAddress End Function