Windows上类似于dig的功能是什么?

11
在Windows系统上,我正在尝试收集DNS名称的所有IP地址,并使用每个IP地址调用一个工具。 我知道如何在shell脚本中实现它,但不知道如何在批处理或PowerShell文件中实现。
我想将其移植到Windows上。
#!/usr/bin/env bash
# Get all the IPs for our server instance
# and pass it to "p4 trust" to update the .p4trust file
for address in $(dig perforce.example.com +short)
do
    echo "processing address: $address:1666"
    p4 -p "ssl:$address:1666" trust -y -f || true
done

问题:

  1. 是否有预安装的Windows dig 等效工具,它将仅返回DNS记录的IP地址?
  2. 在批处理或PowerShell文件中,如何循环迭代另一个应用程序的多个结果?

在Powershell中,可以使用"foreach"命令。此外,你还可以使用"nslookup"和"resolve-dnsname"命令进行DNS解析,不必使用批处理。毕竟现在已经是2019年了。 - 4c74356b41
1个回答

15

试试这个...

Get-Command -Name Resolve-Dns* | 
Format-Table -AutoSize

CommandType Name                   Version Source       
----------- ----                   ------- ------       
Cmdlet      Resolve-DnsName        1.0.0.0 DnsClient     

# Get parameters, examples, full and Online help for a cmdlet or function

(Get-Command -Name Resolve-DnsName).Parameters
Get-help -Name Resolve-DnsName -Examples
Get-help -Name Resolve-DnsName -Full
Get-help -Name Resolve-DnsName -Online


# Get all IPAddresses for the provided DNS name
$env:USERDNSDOMAIN | ForEach{Resolve-DnsName -Name $PSItem}

Name          Type   TTL   Section    IPAddress                                
----          ----   ---   -------    ---------                                
CONTOSO.COM A      600   Answer     192.168....                             
CONTOSO.COM A      600   Answer     10.10... 

# 
$env:USERDNSDOMAIN | 
ForEach{
    # Get all IP addresses for the provided DNS name
    $DNSIPA = (Resolve-DnsName -Name $PSItem).IPAddress
    
    # Check if the host is up for a given IPA and port number
    ForEach($IPA in $DNSIPA)
    {
        "Processing $IPA"
        Test-NetConnection -ComputerName $IPA -Port 1666
    }
}

然而,这个问题意味着您是新手并不熟悉PowerShell。因此,在您造成自己不必要的困惑/挫败等之前,非常重要的是您花时间运用所有可用的免费PowerShell视频和电子书培训资源来提高您的能力。以下仅列举了一些资源。

MSDN, MSDocs, MVA, MSChannel9, YouTube,电子书,使用上面提到的帮助文件。


太好了!这正是我在寻找的。谢谢@postanote! - Jason De Arte
不用担心。很高兴它会有所帮助。只是需要注意的是,Test-NetConnection 并不在所有版本的 PowerShell 中,并且它还要取决于您所使用的操作系统。如果您的 PowerShell/操作系统版本中没有此命令,您需要退而求其次,使用 .Net TCPClient 命名空间来完成这种端口检查的工作。例如:https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/scanning-ports - postanote

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接