VBS 从指定 DNS 获取给定域名(Domain)的 IP 地址


  由域名,查询域名指向的 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值