Advertisement
davsank

NSlookup for Excel

May 28th, 2015
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.11 KB | None | 0 0
  1. 'NSlookup Module by David Sankovsky, G.N.S, May 2015
  2. Public Function NSLookup(lookupVal As String, Optional addressOpt As Integer) As String
  3.    Const ADDRESS_LOOKUP = 1
  4.    Const NAME_LOOKUP = 2
  5.    Const AUTO_DETECT = 0
  6.    
  7.    'Skip everything if the field is blank
  8.    If lookupVal <> "" Then
  9.         Dim oFSO As Object, oShell As Object, oTempFile As Object
  10.         Dim sLine As String, sFilename As String
  11.         Dim intFound As Integer
  12.         Set oFSO = CreateObject("Scripting.FileSystemObject")
  13.         Set oShell = CreateObject("Wscript.Shell")
  14.        
  15.         'Handle the addresOpt operand
  16.         'Regular Expressions are used to complete a substring match for an IP Address
  17.         'If an IP Address is found, a DNS Name Lookup will be forced
  18.         If addressOpt = AUTO_DETECT Then
  19.             ipLookup = FindIP(lookupVal)
  20.             If ipLookup = "" Then
  21.                 addressOpt = ADDRESS_LOOKUP
  22.             Else
  23.                 addressOpt = NAME_LOOKUP
  24.                 lookupVal = ipLookup
  25.             End If
  26.         'Do a regular expression substring match for an IP Address
  27.         ElseIf addressOpt = NAME_LOOKUP Then
  28.             lookupVal = FindIP(lookupVal)
  29.         End If
  30.        
  31.         'Run the nslookup command
  32.         sFilename = oFSO.GetTempName
  33.         oShell.Run "cmd /c nslookup " & lookupVal & " > " & sFilename, 0, True
  34.         Set oTempFile = oFSO.OpenTextFile(sFilename, 1)
  35.         Do While oTempFile.AtEndOfStream <> True
  36.             sLine = oTempFile.Readline
  37.             cmdStr = cmdStr & Trim(sLine) & vbCrLf
  38.         Loop
  39.         oTempFile.Close
  40.         oFSO.DeleteFile (sFilename)
  41.        
  42.         'Process the result
  43.         intFound = InStr(1, cmdStr, "Name:", vbTextCompare)
  44.         If intFound = 0 Then
  45.             NSLookup = "NotFound"
  46.             Exit Function
  47.         ElseIf intFound > 0 Then
  48.             If addressOpt = ADDRESS_LOOKUP Then
  49.                 loc1 = InStr(intFound, cmdStr, "Address:", vbTextCompare) + InStr(intFound, cmdStr, "Addresses:", vbTextCompare)
  50.                 loc2 = InStr(loc1, cmdStr, vbCrLf, vbTextCompare)
  51.                 nameStr = Trim(Mid(cmdStr, loc1 + 8, loc2 - loc1 - 8))
  52.             ElseIf addressOpt = NAME_LOOKUP Then
  53.                 loc1 = InStr(intFound, cmdStr, "Name:", vbTextCompare)
  54.                 loc2 = InStr(loc1, cmdStr, vbCrLf, vbTextCompare)
  55.                 nameStr = Trim(Mid(cmdStr, loc1 + 5, loc2 - loc1 - 5))
  56.             End If
  57.         End If
  58.         NSLookup = nameStr
  59.     Else
  60.         NSLookup = "N/A"
  61.     End If
  62. End Function
  63.  
  64. Function FindIP(strTest As String) As String
  65.     Dim RegEx As Object
  66.     Dim valid As Boolean
  67.     Dim Matches As Object
  68.     Dim i As Integer
  69.     Set RegEx = CreateObject("VBScript.RegExp")
  70.     'Validate return pattern is a valid IPv4 Address Format
  71.     RegEx.Pattern = "\b(?:\d{1,3}\.){3}\d{1,3}\b"
  72.     valid = RegEx.test(strTest)
  73.     If valid Then
  74.         Set Matches = RegEx.Execute(strTest)
  75.         FindIP = Matches(0)
  76.     Else
  77.         FindIP = "" 'Return emptry string if format is mismatched
  78.     End If
  79. End Function
  80. 'NSlookup Module by David Sankovsky, G.N.S, May 2015
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement