簡體   English   中英

IP地址的DNS名稱

[英]DNS name from IP address

我需要創建一個IP地址和DNS名稱列表。 我正在嘗試從IP地址獲取DNS名稱。 我嘗試了兩種方法:

  1. try / catch但此后結束。
  2. 沒有它,它只會輸出與IP地址無關的DNS名稱。

這是我到目前為止的內容:

#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 }
# }

試試這個解決方案:

$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
}

我們創建一個對象,該對象中具有IP地址,如果無法解析則提供主機名n/a 然后,對象被導出到文件中。 您會得到類似的信息:

192.0.0.1; Server1

這使用了工作流程,因此可以並行進行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"

您可能想嘗試這里提供的其他解決方案,但是這里有一些您可能要考慮的事情。

首先,建議不要在整個第一個命令中都使用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
    }
  }
}

當你捕獲異常,你只能寫在“請求的名稱是有效”的情況下的文本文件(你有效期是什么意思?)。 否則,您絕不向文件寫入任何內容。 因此,回到原始代碼:

  • 如果有由任何 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地址和主機名(如果確定)都寫入輸出文件的方法。 我將對此進行查看以弄清楚發生了什么。

我的最后一個提示是要小心使用Get-Content.txt文件中讀取。 它們通常帶有尾隨(空白)行,您可能想嘗試忽略此類空格。 在這種情況下,這可能不是什么大問題,因為這僅意味着DNS嘗試失敗,但是我已經看到,在與其他命令一起使用時,(很多)大公司中的每個郵箱都會遭受嚴重破壞。

其他方式...

$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 }
    }
}

有許多工具可以完成此任務,但是如果您需要一種快速而骯臟的解決方案,那么您可以在幾乎任何地方運行它就可以完成工作。

使用永恆有用的ps工具(例如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

現在,您已經確認此IP地址對於該用戶是正確的。 我們只需要使用nslookup查找IP地址。

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

現在我們知道該計算機的計算機名稱為Workstation07。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM