Necessity is the mother of invention as they say. Today I had to write a couple of scripts to translate DNS names into IP addresses and vice versa. Since there is no easy way to do this in VBScript, I whipped up two (well three) quick little functions that use the nslookup command to preform the lookup using the computers default DNS name servers.
They are pretty straight forward to use, for the DNSLookup function you can pass any IP address and it will try to resolve. If it is successful it will return the fully qualified domain name. (Aliases are not returned). If it fails it will return “Failed.” It also checks the string that you pass with another new function called IsIP. This function checks any string you pass to see if it qualifies as a legit IP. If it is an IP the IsIP function returns “True”. If not then it returns “False”.
The ReverseDNSLookup will translate a fully qualified domain name into an IP address. Or if you have DNS suffix appending turned on then it will resolve computer names that are not fully qualified. If it fails to resolve to an IP the function returns “Failed.”
Function ReverseDNSLookup(sIPAddress) 'This script is provided under the Creative Commons license located 'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not 'be used for commercial purposes with out the expressed written consent 'of NateRice.com If Not IsIP(sIPAddress) Then ReverseDNSLookup = "Failed: Invalid IP." Exit Function End If Const OpenAsDefault = -2 Const FailIfNotExist = 0 Const ForReading = 1 Set oShell = CreateObject("WScript.Shell") Set oFSO = CreateObject("Scripting.FileSystemObject") sTemp = oShell.ExpandEnvironmentStrings("%TEMP%") sTempFile = sTemp & "\" & oFSO.GetTempName oShell.Run "%comspec% /c nslookup " & sIPAddress & ">" & sTempFile, 0, True Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsDefault) sResults = fFile.ReadAll fFile.Close oFSO.DeleteFile (sTempFile) If InStr(sResults, "Name:") Then aNameTemp = Split(sResults, "Name:") aName = Split(Trim(aNameTemp(1)), Chr(13)) ReverseDNSLookup = aName(0) Else ReverseDNSLookup = "Failed." End If Set oShell = Nothing Set oFSO = Nothing End Function Function DNSLookup(sAlias) 'This script is provided under the Creative Commons license located 'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not 'be used for commercial purposes with out the expressed written consent 'of NateRice.com If len(sAlias) = 0 Then DNSLookup = "Failed." Exit Function End If Const OpenAsDefault = -2 Const FailIfNotExist = 0 Const ForReading = 1 Set oShell = CreateObject("WScript.Shell") Set oFSO = CreateObject("Scripting.FileSystemObject") sTemp = oShell.ExpandEnvironmentStrings("%TEMP%") sTempFile = sTemp & "\" & oFSO.GetTempName oShell.Run "%comspec% /c nslookup " & sAlias & ">" & sTempFile, 0, True Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsDefault) sResults = fFile.ReadAll fFile.Close oFSO.DeleteFile (sTempFile) aIP = Split(sResults, "Address:") If UBound(aIP) < 2 Then DNSLookup = "Failed." Else aIPTemp = Split(aIP(2), Chr(13)) DNSLookup = trim(aIPTemp(0)) End If Set oShell = Nothing Set oFSO = Nothing End Function Function IsIP(sIPAddress) 'This script is provided under the Creative Commons license located 'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not 'be used for commercial purposes with out the expressed written consent 'of NateRice.com aOctets = Split(sIPAddress,".") If IsArray(aOctets) Then If UBound(aOctets) = 3 Then For Each sOctet In aOctets On Error Resume Next sOctet = Trim(sOctet) sOctet = sOctet + 0 On Error Goto 0 If IsNumeric(sOctet) Then If sOctet < 0 Or sOctet > 256 Then IsIP = False Exit Function End If Else IsIP = False Exit Function End If Next IsIP = True Else IsIP = False End If Else IsIP = False End If End Function
it goes wrong when a dns name has an IP6 and an IP4 🙁
Post a patch. Or post an example of when it has an issue.