由域名,查询域名指向的 IP 地址,从指定的 DNS 服务器获取数据。实现这一功能,使用的是 nslookup 这条 command 命令,在 vbs 中调用 WScript.Exec 来运行命令,从 StdOut 中读出命令输出文本流。最后从输出的文本中,提取目标 IP 地址。
代码中的 anti popup cmd window 部分,用以避免 WScript.Exec 弹出的 cmd 窗口。因为 WScript.Run 可以指定窗口显示样式,可以选择不显示窗口。
当 WScript.Run 时,使用 msgbox 替代 WScript.Echo,后者将不会弹出消息窗口
Option Explicit
Dim dns
dns = "119.29.29.29"
Dim regEx
Set regEx = New RegExp
antiPopupWindow
msgbox getDomainIp(dns, "bing.com")
Sub antiPopupWindow
'anti popup cmd window
Dim ws, host
Set ws = CreateObject("WScript.Shell")
host = WScript.FullName
'WScript.Echo(host)
regEx.Pattern = "wscript.exe"
regEx.IgnoreCase = True
If regEx.test(host) Then
' WScript.Echo("cscript.exe """ & WScript.ScriptFullName & """")
ws.run "cscript """ & WScript.ScriptFullName & """", 0
WScript.Quit
End If
End Sub
Function getDomainIp(dns, domain)
'Begin lookup domain IP by command nslookup
Dim objShell, objExecObject, strText
Set objShell = CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("%comspec% /c nslookup " & domain & " " & dns)
Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadAll()
Loop
'msgbox strText
'search target IP
Dim Matches, Match, domainIp
regEx.Pattern = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
regEx.IgnoreCase = False
regEx.Global = True
Set Matches = regEx.Execute(strText)
If Matches.Count > 1 Then
domainIp = Matches.Item(1).Value
End If
'msgbox domainIp
getDomainIp = domainIp
End Function