$ErrorActionPreference = 'SilentlyContinue' $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path $root = (Get-Item (Join-Path $scriptDir '..\..')).FullName $adb = Join-Path $root 'lib\scrcpy-adb\adb.exe' if (-not (Test-Path $adb)) { Write-Host '[ERROR] ADB not found'; exit 1 } $lines = & $adb devices 2>$null $dev = $lines | ForEach-Object { $t = $_.Trim(); if ($t -match '^(\S+)\s+device') { $Matches[1] } } | Where-Object { $_ } | Select-Object -First 1 if (-not $dev) { Write-Host 'no device'; exit 0 } $ip = (& $adb -s $dev shell getprop dhcp.wlan0.ipaddress 2>$null).Trim() if ($ip -notmatch '^\d+\.\d+\.\d+\.\d+$') { $ip = (& $adb -s $dev shell getprop dhcp.eth0.ipaddress 2>$null).Trim() } if ($ip -notmatch '^\d+\.\d+\.\d+\.\d+$') { $out = & $adb -s $dev shell 'ip route get 1.1.1.1' 2>$null if ($out -match '\bsrc\s+(\d+\.\d+\.\d+\.\d+)\b') { $ip = $Matches[1] } } if ($ip -notmatch '^\d+\.\d+\.\d+\.\d+$') { $out = & $adb -s $dev shell 'ip -4 addr show wlan0' 2>$null if ($out -match 'inet\s+(\d+\.\d+\.\d+\.\d+)') { $ip = $Matches[1] } } if ($ip -notmatch '^\d+\.\d+\.\d+\.\d+$') { $out = & $adb -s $dev shell 'ip -4 addr show eth0' 2>$null if ($out -match 'inet\s+(\d+\.\d+\.\d+\.\d+)') { $ip = $Matches[1] } } if ($ip -notmatch '^\d+\.\d+\.\d+\.\d+$') { $out = & $adb -s $dev shell 'ip -4 addr show' 2>$null $ips = [regex]::Matches($out, 'inet\s+(\d+\.\d+\.\d+\.\d+)') | ForEach-Object { $_.Groups[1].Value } $ip = $ips | Where-Object { $_ -ne '127.0.0.1' } | Select-Object -First 1 } if ($ip) { Write-Host $ip } else { Write-Host 'failed' }