powershell 将“nslookup主机”与“nslookup ip”进行比较

gk7wooem  于 2023-01-30  发布在  Shell
关注(0)|答案(2)|浏览(106)

我需要使用标准的Windows工具(CMD、VBS、POWERSHELL)确定主机的DNS查找是否与Windows平台上IP的DNS查找输出相匹配。
举个例子-
1.使用NSLOOKUP查询主机名(haw1

C:>nslookup haw1
Server:  myserver.blah.org
Address:  172.22.14.10

Name:    haw1.blah.org
Address:  172.40.82.70

1.然后从#1中的结果查询IP地址

C:>nslookup 172.40.82.70
Server:  myserver.blah.org
Address:  172.22.14.10

Name:    ghi1.blah.org
Address:  172.40.82.70

1.比较结果以查看它们是否匹配。在本例中www.example.com〈〉www.example.com haw1.blah.org <> ghi.blah.org
1.根据结果输出类似"www.example.com不匹配"或"haw1.blah.org匹配"的内容。 haw1.blah.org does not match" or "haw1.blah.org matches" depending on the results.
你知道怎么做吗?
加上我目前掌握的...
这给我留下了项目的FQDN和名称从DNS返回的IP。我不知道如何继续进行反向DNS查找和比较。
我有的只是部分解决办法。

if exist name.txt del name.txt
if exist address.txt del address.txt
nslookup %1 > out.txt
type out.txt | find /v /i "server" >out1.txt
del out.txt
type out1.txt | find /v /i "172.22.14.10" >out.txt
del out1.txt
type out.txt | find /i "name" > name.txt
type out.txt | find /i "address:" > address.txt
del out.txt
for /f "tokens=2" %%i in (name.txt) do echo %%i >name1.txt
del name.txt
for /f "tokens=2" %%i in (address.txt) do echo %%i >address1.txt
del address.txt
rseugnpd

rseugnpd1#

此示例将要求您键入或粘贴远程计算机名,并报告是否存在IP不匹配。

Clear-Host 
$ComputerName = Read-Host -Prompt "IP Mismatch? Type or Paste a computer name here to detect >"

$NameLookup = [Net.DNS]::GetHostEntry($ComputerName)
$IPAddress = @($NameLookup.AddressList)[0].IPAddressToString
$IPLookup = [Net.DNS]::GetHostEntry($IPAddress)

$_N = $NameLookup.HostName.ToString()
$_NIP = $NameLookup.AddressList.IPAddressToString
$_R = $IPLookup.HostName.ToString()
$_RIP = $IPLookup.AddressList.IPAddressToString
If ($_N -eq $_R ) {

    Write-Host "NO MISMATCH!`n$_N,$_NIP`n$_R,$_RIP"

}
Else {

    Write-Host "WARNING MISMATCH!`n$_N,$_NIP`n$_R,$_RIP"

}

#Original Source from
#https://stackoverflow.com/questions/42655065/compare-nslookup-host-to-nslookup-ip
#Modified by John Delise 2022-01-01 Happy New Year
rggaifut

rggaifut2#

正如TessellingHeckler所指出的,这种方法在很多情况下都会失败。尽管如此,下面是一个PowerShell方法,它使用了来自.Net的Net.DNS,应该可以在Windows 7上使用:

$ComputerName = "haw1"
$NameLookup = [Net.DNS]::GetHostEntry($ComputerName)
$IPAddress = @($NameLookup.AddressList)[0].IPAddressToString
$IPLookup = [Net.DNS]::GetHostEntry($IPAddress)
$NameLookup.HostName -eq $IPLookup.HostName

相关问题