Skip to content

Instantly share code, notes, and snippets.

@Rapidhands
Created October 23, 2024 06:33
Show Gist options
  • Save Rapidhands/a91fdc3fed37755aa406f722927e3cf5 to your computer and use it in GitHub Desktop.
Save Rapidhands/a91fdc3fed37755aa406f722927e3cf5 to your computer and use it in GitHub Desktop.
Get Geolocation of the DNS Root servers
# DNS Root list
$rootServers = @(
"a.root-servers.net",
"b.root-servers.net",
"c.root-servers.net",
"d.root-servers.net",
"e.root-servers.net",
"f.root-servers.net",
"g.root-servers.net",
"h.root-servers.net",
"i.root-servers.net",
"j.root-servers.net",
"k.root-servers.net",
"l.root-servers.net",
"m.root-servers.net"
)
# Function to get location from an IP address
function Get-GeoLocation {
param(
[string]$ipAddress
)
try {
# Request to IPify API
$IPInfo = Invoke-RestMethod -Method Get -Uri "http://ip-api.com/json/$ipAddress" -ErrorAction stop
$IPInfo
}
catch {
Write-Warning "Error retrieving location for IP address $ipAddress"
return "Unknown"
}
}
# Initializing the variable to store the results
$result = [System.Collections.Generic.List[PSObject]]::new()
# Loop through each root DNS server
foreach ($server in $rootServers) {
try {
# Resolve domain name to get IP address
$ipAddress = (Resolve-DnsName $server -Type A | Select-Object IPAddress -First 1).IpAddress
# Get location
$location = Get-GeoLocation -ipAddress $ipAddress
# Build a PSCustomObject with the desired info. Feel free to add more properties
$Obj = [PSCustomObject]@{
DNSRootServer = $server
DNSRootIP = $location.query
Location = $location.country
}
# Add $Obj to $result
$result.add($Obj)
}
catch {
Write-Warning "Error resolving domain name $server"
}
}
# Displaying results
$result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment