50 lines
1.5 KiB
PowerShell
50 lines
1.5 KiB
PowerShell
# 실제 LAN IP (Docker/Hyper-V 172.17.x 등 가상 어댑터 제외)
|
|
param(
|
|
[string]$PreferredPrefix = '172.16.'
|
|
)
|
|
|
|
$override = $env:EENE_LAN_IP
|
|
if ($override) {
|
|
Write-Output $override.Trim()
|
|
exit 0
|
|
}
|
|
|
|
function Test-PhysicalAdapter {
|
|
param([int]$IfIndex)
|
|
$adapter = Get-NetAdapter -InterfaceIndex $IfIndex -ErrorAction SilentlyContinue
|
|
if (-not $adapter -or $adapter.Status -ne 'Up') { return $false }
|
|
$desc = $adapter.InterfaceDescription
|
|
if ($desc -match 'Virtual|Hyper-V|Docker|WSL|vEthernet|VMware|Loopback|TAP|TUN|VPN') { return $false }
|
|
return $true
|
|
}
|
|
|
|
$candidates = Get-NetIPAddress -AddressFamily IPv4 -ErrorAction SilentlyContinue |
|
|
Where-Object {
|
|
$_.IPAddress -match '^(172\.(1[6-9]|2[0-9]|3[01])\.|192\.168\.|10\.)' -and
|
|
$_.IPAddress -notmatch '^169\.254\.' -and
|
|
$_.IPAddress -notmatch '^172\.17\.' -and
|
|
(Test-PhysicalAdapter -IfIndex $_.InterfaceIndex)
|
|
}
|
|
|
|
if (-not $candidates) {
|
|
$candidates = Get-NetIPAddress -AddressFamily IPv4 -ErrorAction SilentlyContinue |
|
|
Where-Object {
|
|
$_.IPAddress -match '^(172\.|192\.168\.|10\.)' -and
|
|
$_.IPAddress -notmatch '^169\.254\.' -and
|
|
$_.IPAddress -notmatch '^172\.17\.'
|
|
}
|
|
}
|
|
|
|
$ip = $candidates |
|
|
Sort-Object @{
|
|
Expression = {
|
|
if ($_.IPAddress -like "$PreferredPrefix*") { 0 }
|
|
elseif ($_.IPAddress -match '^192\.168\.') { 1 }
|
|
elseif ($_.IPAddress -match '^10\.') { 2 }
|
|
else { 3 }
|
|
}
|
|
}, @{ Expression = { $_.PrefixLength } } |
|
|
Select-Object -First 1 -ExpandProperty IPAddress
|
|
|
|
if ($ip) { Write-Output $ip }
|