来自IP地址的DNS名称

时间:2017-06-06 18:56:31

标签: powershell

我需要创建一个IP地址和DNS名称列表。一世    我试图从IP地址获取DNS名称。我尝试了两种方法:

  1. try / catch但之后结束。
  2. 没有,它只输出我无法与IP地址相关的DNS名称。
  3. 这是我到目前为止所拥有的:

    #try {
     Get-Content C:\Users\pintose\Documents\IP_Address.txt | ForEach-Object 
    {([system.net.dns]::GetHostByAddress($_)).hostname >> C:\Users\user\Documents\hostname.txt}
    # }
     # catch {
     if ($_.Exception.Message -like "*The requested name is valid*") {
                Write-Output "UNREACHABLE" | Out-File C:\Users\user\Documents\hostname.txt }
    # }
    

5 个答案:

答案 0 :(得分:1)

试试这个解决方案:

$outFile = "C:\Users\user\Documents\hostname.txt"

Get-Content C:\Users\pintose\Documents\IP_Address.txt | ForEach-Object {
    $hash = @{ IPAddress = $_
        hostname = "n/a"     
    }
    $hash.hostname = ([system.net.dns]::GetHostByAddress($_)).hostname
    $object = New-Object psobject -Property $hash
    Export-CSV -InputObject $object -Path $outFile -Append -NoTypeInformation
}

我们创建一个对象,其中包含IPaddress,如果无法解析,则创建主机名n/a。然后,对象被导出到文件中。你会得到类似的东西:

192.0.0.1; Server1

答案 1 :(得分:0)

这使用工作流程,因此它可以执行并行的foreach

Workflow Get-DNSNames([string[]]$IPAddresses){
    foreach -parallel ($IP in $IPAddresses){
        try{
            @{$IP = $(([system.net.dns]::GetHostByAddress($IP)).hostname)}
        }catch{
            @{$IP = "N/A"}
        }
    }
}

$List = Get-DNSNames -IPAddresses $(Get-Content "C:\IPAddresses.txt").Split("[\r\n]")
$List | Out-File "C:\IPAddresses_Complete.txt"

答案 2 :(得分:0)

您可能想尝试这里提供的其他解决方案,但这里有一些您可能想要考虑的事项。

首先,我建议try{}catch{}放在第一个命令的整个周围。如果您循环访问数据并且只有其中一个导致异常,则可能无法完成任务。将try{}catch{}放在"冒险"代码行:

Get-Content C:\Users\pintose\Documents\IP_Address.txt | Foreach-Object {
  try {
    ([system.net.dns]::GetHostByAddress($_)).hostname >> C:\Users\user\Documents\hostname.txt
  }
  catch {
    if ($_.Exception.Message -like "*The requested name is valid*") {
      Write-Output "UNREACHABLE" | Out-File C:\Users\user\Documents\hostname.txt
    }
  }
}

当您捕获异常时,只有在"所请求的名称有效且"的情况下,您才会写入文本文件。 (你的意思是 有效?)。否则你永远不会写任何文件。因此,回到原始代码:

  • 如果由 ANY 的IP地址引起异常
  • 并且例外是"请求的名称有效" (我认为这可能是一个错字?)
  • 然后没有错误写入文件,脚本结束时无需填写所有IP地址。

其他事项:

您使用两种方法写入文件:>>Out-File。可能更好的方法是使用PowerShell cmdlet但使用-Append开关来确保附加到文件的末尾:

([system.net.dns]::GetHostByAddress($_)).hostname | Out-File C:\Users\user\Documents\hostname.txt -Append

Write-Output "UNREACHABLE" | Out-File C:\Users\user\Documents\hostname.txt -Append

@ restless1987建议了一种确保将IP地址和主机名(如果确定)写入输出文件的方法。我要看一下,看看发生了什么。

我的最后一个提示是要谨慎使用.txtGet-Content文件中读取。它们通常具有尾随(空白)行,您可能想要尝试忽略这些空白。在这种情况下可能不是一个大问题,因为它只是意味着DNS尝试失败,但我已经看到这样的事情在与其他命令一起使用时对(非常)大公司的每个邮箱造成严重破坏。

答案 3 :(得分:0)

另一种方式......

$ip_list = Get-Content ./IP_Address.txt

foreach ($ip in $ip_list) {
    try {
        ([system.net.dns]::GetHostByAddress($ip)).hostname |
            Out-File -FilePath ./hostname.txt -Append
    }
    catch {
        if ($_.Exception.Message -like "*The requested name is valid*") {
            Write-Output "UNREACHABLE" | Out-File -FilePath './hostname.txt' -Append }
    }
}

答案 4 :(得分:0)

有许多工具可以实现这一目标,但如果你需要一个快速而肮脏的解决方案,你可以在任何地方运行,这将完成工作。

使用永久有用的ps tools psloggedon /accepteula \\computername or ip address,例如c:\pstools>psloggedon /accepteula \\10.0.0.10 loggedon v1.33 - See who's logged on Copyright ⌐ 2000-2006 Mark Russinovich Sysinternals - www.sysinternals.com Users logged on locally: Error: could not retrieve logon time NT AUTHORITY\LOCAL SERVICE Error: could not retrieve logon time NT AUTHORITY\NETWORK SERVICE 1/12/2015 8:06:51 AM DOMAIN\user Error: could not retrieve logon time NT AUTHORITY\SYSTEM Users logged on via resource shares: 1/17/2015 2:26:43 PM DOMAIN\user ,您可以获取当前登录的用户,以检查这是否是正确的机器。例如:

C:\>nslookup 10.0.0.10
Server:  server.domain.com
Address:  10.10.0.1

Name:    Workstation07.server.domain.com
Address:  10.0.0.10

现在您已确认此IP地址是此用户的正确IP地址。我们只需要使用nslookup查找IP地址。

<paper>
    <header>
    </header>
    <body>
        <paragraph>
        </paragraph>
    </body>
    <conclusion>
    </conclusion>
</paper>

现在我们知道该计算机的计算机名称是Workstation07。

相关问题