PowerShell/scripts/list-ip-addresses.ps1

60 lines
1.9 KiB
PowerShell
Raw Normal View History

2023-10-31 12:33:36 +01:00
<#
2023-08-22 08:15:53 +02:00
.SYNOPSIS
2024-06-18 20:22:51 +02:00
Lists the IP addresses
2023-08-22 08:15:53 +02:00
.DESCRIPTION
2024-06-18 20:22:51 +02:00
This PowerShell script queries all IP address information and prints it.
2023-08-22 08:15:53 +02:00
.EXAMPLE
2024-06-18 20:22:51 +02:00
PS> ./list-ip-addresses.ps1
2024-07-23 17:40:32 +02:00
Public IP 185.72.229.161, 2003:f2:6128:fd01:e543:601:30c2:a028 near Munich, Germany
2023-08-22 08:15:53 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2024-06-18 20:22:51 +02:00
function WriteLocalInterface($interface) {
2024-06-18 20:35:42 +02:00
$IPv4 = $IPv6 = $prefixLen = ""
2024-06-18 20:22:51 +02:00
$addresses = Get-NetIPAddress
foreach ($addr in $addresses) {
if ($addr.InterfaceAlias -like "$($interface)*") {
if ($addr.AddressFamily -eq "IPv4") {
$IPv4 = $addr.IPAddress
2024-06-18 20:35:42 +02:00
$prefixLen = $addr.PrefixLength
2024-06-18 20:22:51 +02:00
} else {
$IPv6 = $addr.IPAddress
}
}
}
if ($IPv4 -ne "" -or $IPv6 -ne "") {
2024-07-23 17:40:32 +02:00
Write-Host "✅ Local $interface IP $IPv4/$prefixLen, $IPv6"
2024-06-18 20:22:51 +02:00
}
}
2023-08-22 08:15:53 +02:00
try {
2024-06-18 20:25:54 +02:00
if (!$IsLinux) {
WriteLocalInterface "Ethernet"
WriteLocalInterface "WLAN"
WriteLocalInterface "Bluetooth"
}
2023-08-22 08:15:53 +02:00
if ($IsLinux) {
2023-11-27 12:51:01 +01:00
[string]$publicIPv4 = (curl -4 --silent ifconfig.co)
[string]$publicIPv6 = (curl -6 --silent ifconfig.co)
[string]$city = (curl --silent ifconfig.co/city)
[string]$country = (curl --silent ifconfig.co/country)
2023-08-22 08:15:53 +02:00
} else {
2023-11-27 12:51:01 +01:00
[string]$publicIPv4 = (curl.exe -4 --silent ifconfig.co)
[string]$publicIPv6 = (curl.exe -6 --silent ifconfig.co)
[string]$city = (curl.exe --silent ifconfig.co/city)
[string]$country = (curl.exe --silent ifconfig.co/country)
2023-08-22 08:15:53 +02:00
}
2023-09-13 08:41:22 +02:00
if ("$publicIPv4" -eq "") { $publicIPv4 = "no IPv4" }
if ("$publicIPv6" -eq "") { $publicIPv6 = "no IPv6" }
2023-11-27 12:51:01 +01:00
if ("$city" -eq "") { $city = "unknown city" }
if ("$country" -eq "") { $country = "unknown country" }
2024-07-23 17:40:32 +02:00
Write-Host "✅ Public IP $publicIPv4, $publicIPv6 near $city, $country"
2023-08-22 08:15:53 +02:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}