Assistance at your fingertips...

Reverse /24 IP Scope With PTR and Hostname

'check-24RangeInUse.ps1'
#Function-Get24IPRange.ps1
function Get-24IPRange {
    param(
        [string]$subnetBase
    )

    $baseSplit = $subnetBase -split '\.'
    $baseIP = [System.Net.IPAddress]::Parse($subnetBase).GetAddressBytes()

    [Array]::Reverse($baseIP)
    $baseDecimal = [BitConverter]::ToUInt32($baseIP, 0)

    for ($i = 0; $i -lt 256; $i++) { # 256 for /24 subnet
        $currentDecimal = $baseDecimal + $i
        $bytes = [BitConverter]::GetBytes($currentDecimal)
        [Array]::Reverse($bytes)
        $ip = [System.Net.IPAddress]::new($bytes).IPAddressToString
        Write-Output $ip
    }
}

$scopetocheck = Read-Host -prompt "What /24 range?, like 8.8.8.0"
$ipaddresses = Get-24IPRange $scopetocheck
foreach($ipaddress in $ipaddresses){if((test-connection $ipaddress.ToString() -count 1 -Quiet) -eq $true){
$reverseDnsLookup = (Resolve-DnsName -Name $ipAddress -Type PTR -NoHostsFile -NetbiosFallback:$false -QuickTimeout).Namehost
if(!($reverseDnsLookup)){$ipaddress;continue}
"$reverseDnsLookup is $ipaddress"
}
}

Posted

in

by

Tags: