-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule5-NSLookup_NS
77 lines (52 loc) · 2.53 KB
/
Module5-NSLookup_NS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'2019-04-22 RookieITSec note - This script was created by somoene else and modified by me. Found here - https://www.geekality.net/2016/03/07/excel-function-for-nslookup-in-worksheet/ and here https://github.com/cscholz/scripts/blob/master/Windows/VBA/Excel/nslookup.txt
'2019-04-22 RookieITSec note -This pulls the first nameserver for a domain. Most domains have multiple nameservers, but I only needed the first for validation generally.
Public Function NSLookup_NS(lookupVal As String, Optional addressOpt As Integer) As String
Const ADDRESS_LOOKUP = 1
Const NAME_LOOKUP = 2
Const AUTO_DETECT = 0
'Skip everything if the field is blank
If lookupVal <> "" Then
Dim oFSO As Object, oShell As Object, oTempFile As Object
Dim sLine As String, sFilename As String
Dim intFound As Integer
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("Wscript.Shell")
'Handle the addresOpt operand
'Regular Expressions are used to complete a substring match for an IP Address
'If an IP Address is found, a DNS Name Lookup will be forced
If addressOpt = AUTO_DETECT Then
ipLookup = FindIP(lookupVal)
If ipLookup = "" Then
addressOpt = ADDRESS_LOOKUP
Else
addressOpt = NAME_LOOKUP
lookupVal = ipLookup
End If
'Do a regular expression substring match for an IP Address
ElseIf addressOpt = NAME_LOOKUP Then
lookupVal = FindIP(lookupVal)
End If
'Run the nslookup command
sFilename = oFSO.GetTempName
oShell.Run "cmd /c nslookup -q=ns " & lookupVal & " 8.8.8.8 " & " > " & sFilename, 0, True
Set oTempFile = oFSO.OpenTextFile(sFilename, 1)
Do While oTempFile.AtEndOfStream <> True
sLine = oTempFile.Readline
cmdStr = cmdStr & Trim(sLine) & vbCrLf
Loop
oTempFile.Close
oFSO.DeleteFile (sFilename)
'Process the result
intFound = InStr(1, cmdStr, "nameserver = ", vbTextCompare)
If intFound = 0 Then
NSLookup_NS = " Exit Function"
ElseIf intFound > 0 Then
'TODO: Cleanup with RegEx
loc1 = intFound
loc2 = InStr(loc1, cmdStr, vbNewLine, vbTextCompare)
nameStr = Trim(Mid(cmdStr, loc1 + 13, loc2 - loc1 - 13))
End If
NSLookup_NS = nameStr
Else
End If
End Function