Private Sub Command1_Click()
'****************************************************************
' 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)
'
'****************************************************************
'Modified 2/2/02 to just show the IP addresses - Doug Knox
'Original script located at: http://home.istar.ca/~neutron/wsh/IPInfo/getipname.html
arAddresses = GetIPAddresses()
info = ""
For Each ip In arAddresses
info = info & ip & vbTab & GetFQDN(ip) & vbCr
Next
Print "IP address="; info
End Sub
Public 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
Public Function GetIPAddresses()
Dim arIPAddress()
'=====
' 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