getip.ps1 1.5 KB

12345678910111213141516171819202122232425262728
  1. $ErrorActionPreference = 'SilentlyContinue'
  2. $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  3. $root = (Get-Item (Join-Path $scriptDir '..\..')).FullName
  4. $adb = Join-Path $root 'lib\scrcpy-adb\adb.exe'
  5. if (-not (Test-Path $adb)) { Write-Host '[ERROR] ADB not found'; exit 1 }
  6. $lines = & $adb devices 2>$null
  7. $dev = $lines | ForEach-Object { $t = $_.Trim(); if ($t -match '^(\S+)\s+device') { $Matches[1] } } | Where-Object { $_ } | Select-Object -First 1
  8. if (-not $dev) { Write-Host 'no device'; exit 0 }
  9. $ip = (& $adb -s $dev shell getprop dhcp.wlan0.ipaddress 2>$null).Trim()
  10. if ($ip -notmatch '^\d+\.\d+\.\d+\.\d+$') { $ip = (& $adb -s $dev shell getprop dhcp.eth0.ipaddress 2>$null).Trim() }
  11. if ($ip -notmatch '^\d+\.\d+\.\d+\.\d+$') {
  12. $out = & $adb -s $dev shell 'ip route get 1.1.1.1' 2>$null
  13. if ($out -match '\bsrc\s+(\d+\.\d+\.\d+\.\d+)\b') { $ip = $Matches[1] }
  14. }
  15. if ($ip -notmatch '^\d+\.\d+\.\d+\.\d+$') {
  16. $out = & $adb -s $dev shell 'ip -4 addr show wlan0' 2>$null
  17. if ($out -match 'inet\s+(\d+\.\d+\.\d+\.\d+)') { $ip = $Matches[1] }
  18. }
  19. if ($ip -notmatch '^\d+\.\d+\.\d+\.\d+$') {
  20. $out = & $adb -s $dev shell 'ip -4 addr show eth0' 2>$null
  21. if ($out -match 'inet\s+(\d+\.\d+\.\d+\.\d+)') { $ip = $Matches[1] }
  22. }
  23. if ($ip -notmatch '^\d+\.\d+\.\d+\.\d+$') {
  24. $out = & $adb -s $dev shell 'ip -4 addr show' 2>$null
  25. $ips = [regex]::Matches($out, 'inet\s+(\d+\.\d+\.\d+\.\d+)') | ForEach-Object { $_.Groups[1].Value }
  26. $ip = $ips | Where-Object { $_ -ne '127.0.0.1' } | Select-Object -First 1
  27. }
  28. if ($ip) { Write-Host $ip } else { Write-Host 'failed' }