-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-ReverseDns.ps1
More file actions
148 lines (124 loc) · 4.4 KB
/
Copy pathGet-ReverseDns.ps1
File metadata and controls
148 lines (124 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
function Get-ReverseDns
{
<#
.SYNOPSIS
Performs reverse DNS (PTR) lookups for IP addresses.
.DESCRIPTION
Resolves IP addresses to their associated hostnames using reverse DNS (PTR) lookups.
Uses the .NET System.Net.Dns class for cross-platform compatibility. Supports both
IPv4 and IPv6 addresses, pipeline input, and batch processing.
Compatible with PowerShell Desktop 5.1+ on Windows, macOS, and Linux.
.PARAMETER IPAddress
The IP address(es) to perform reverse DNS lookups on.
Accepts both IPv4 and IPv6 addresses. Supports pipeline input.
.EXAMPLE
PS > Get-ReverseDns -IPAddress '8.8.8.8'
IPAddress Hostname Status
--------- -------- ------
8.8.8.8 dns.google Resolved
Performs a reverse DNS lookup for Google's public DNS server.
.EXAMPLE
PS > Get-ReverseDns -IPAddress '1.1.1.1', '8.8.8.8'
IPAddress Hostname Status
--------- -------- ------
1.1.1.1 one.one.one.one Resolved
8.8.8.8 dns.google Resolved
Performs reverse DNS lookups for multiple IP addresses.
.EXAMPLE
PS > '1.1.1.1', '9.9.9.9' | Get-ReverseDns
IPAddress Hostname Status
--------- -------- ------
1.1.1.1 one.one.one.one Resolved
9.9.9.9 dns9.quad9.net Resolved
Pipeline input of multiple IP addresses.
.EXAMPLE
PS > Get-ReverseDns -IPAddress '192.0.2.1'
IPAddress Hostname Status
--------- -------- ------
192.0.2.1 NotFound
Shows output when no PTR record exists for an IP.
.OUTPUTS
PSCustomObject
Returns objects with IPAddress, Hostname, and Status properties.
Status is 'Resolved' when a PTR record is found, 'NotFound' when no record exists,
or 'Error' when the lookup fails.
.NOTES
Author: Jon LaBelle
License: MIT
Source: https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/NetworkAndDns/Get-ReverseDns.ps1
.LINK
https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/NetworkAndDns/Get-ReverseDns.ps1
#>
[CmdletBinding()]
[OutputType([PSCustomObject])]
param
(
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
[ValidateScript({
if (-not ([System.Net.IPAddress]::TryParse($_, [ref]$null)))
{
throw "'$_' is not a valid IP address."
}
return $true
})]
[String[]]
$IPAddress
)
begin
{
Write-Verbose 'Starting reverse DNS lookups'
}
process
{
foreach ($ip in $IPAddress)
{
Write-Verbose "Performing reverse DNS lookup for '$ip'"
try
{
$hostEntry = [System.Net.Dns]::GetHostEntry($ip)
$hostname = $hostEntry.HostName
# If the hostname is just the IP address echoed back, there is no PTR record
if ($hostname -eq $ip)
{
Write-Verbose "No PTR record found for '$ip' (hostname equals IP)"
[PSCustomObject]@{
IPAddress = $ip
Hostname = $null
Status = 'NotFound'
}
}
else
{
Write-Verbose "Resolved '$ip' to '$hostname'"
[PSCustomObject]@{
IPAddress = $ip
Hostname = $hostname
Status = 'Resolved'
}
}
}
catch [System.Net.Sockets.SocketException]
{
Write-Verbose "No PTR record found for '$ip': $($_.Exception.Message)"
[PSCustomObject]@{
IPAddress = $ip
Hostname = $null
Status = 'NotFound'
}
}
catch
{
Write-Verbose "Error resolving '$ip': $($_.Exception.Message)"
[PSCustomObject]@{
IPAddress = $ip
Hostname = $null
Status = 'Error'
}
}
}
}
end
{
Write-Verbose 'Reverse DNS lookups completed'
}
}