diff options
| author | root <root@dangofactory.local> | 2026-11-23 10:20:49 +0900 |
|---|---|---|
| committer | root <root@dangofactory.local> | 2026-11-23 10:20:49 +0900 |
| commit | a67bdcc65a165d1da57ae6d6fead84a1d2e5b248 (patch) | |
| tree | d0075dacac62d509a09974c1cd2f18ca72cf0e39 /pc/pc-autoconnect.ps1 | |
| download | tethering-app-main.tar.gz tethering-app-main.zip | |
Diffstat (limited to 'pc/pc-autoconnect.ps1')
| -rw-r--r-- | pc/pc-autoconnect.ps1 | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/pc/pc-autoconnect.ps1 b/pc/pc-autoconnect.ps1 new file mode 100644 index 0000000..c58da82 --- /dev/null +++ b/pc/pc-autoconnect.ps1 @@ -0,0 +1,115 @@ +#Requires -Version 5.1 +<# + 701KC USB tethering - PC side auto connect + + Run once as Administrator. The RNDIS adapter is switched to DHCP, so from + then on Windows gets 192.168.42.x + gateway 192.168.42.129 automatically + every time the phone enables USB tethering. + + If DHCP does not work on your machine, use: + .\pc-autoconnect.ps1 -Static + which sets 192.168.42.1/24 and default gateway 192.168.42.129. + + To undo: + Set-NetIPInterface -InterfaceIndex <ifIndex> -Dhcp Enabled + Set-NetIPInterface -InterfaceIndex <ifIndex> -InterfaceMetric 25 +#> +[CmdletBinding()] +param( + [switch]$Static, + [string]$PhoneIp = '192.168.42.129' +) + +$ErrorActionPreference = 'Stop' + +function Test-Admin { + $identity = [Security.Principal.WindowsIdentity]::GetCurrent() + $principal = New-Object Security.Principal.WindowsPrincipal($identity) + return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) +} + +if (-not (Test-Admin)) { + Write-Host '管理者権限で再起動します...' + $argList = @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', "`"$PSCommandPath`"") + if ($Static) { $argList += '-Static' } + Start-Process powershell.exe -Verb RunAs -ArgumentList $argList + exit +} + +Write-Host '=== 701KC tethering: PC auto connect ===' + +$adapters = @(Get-NetAdapter | Where-Object { $_.InterfaceDescription -match 'Remote NDIS|RNDIS' }) +if ($adapters.Count -eq 0) { + Write-Error 'RNDIS アダプタが見つかりません。USB を接続し、スマホ側でテザリングを ON にしてください。' + exit 1 +} + +$adapter = $adapters | Where-Object { $_.Status -eq 'Up' } | Select-Object -First 1 +if (-not $adapter) { + $adapter = $adapters | Select-Object -First 1 + Write-Warning "RNDIS アダプタ '$($adapter.Name)' は Up ではありません。テザリングを ON にしてから再実行してください。" +} + +Write-Host "Adapter : $($adapter.Name) [$($adapter.InterfaceDescription)] ifIndex=$($adapter.ifIndex) status=$($adapter.Status)" + +try { + Set-NetConnectionProfile -InterfaceIndex $adapter.ifIndex -NetworkCategory Private -ErrorAction Stop + Write-Host 'Network category: Private' +} catch { + Write-Warning "ネットワーク種別を Private にできませんでした: $($_.Exception.Message)" +} + +if ($Static) { + Write-Host 'Mode : Static 192.168.42.1/24, gateway 192.168.42.129' + Get-NetIPAddress -InterfaceIndex $adapter.ifIndex -AddressFamily IPv4 -ErrorAction SilentlyContinue | + Remove-NetIPAddress -Confirm:$false -ErrorAction SilentlyContinue + Get-NetRoute -InterfaceIndex $adapter.ifIndex -AddressFamily IPv4 -ErrorAction SilentlyContinue | + Remove-NetRoute -Confirm:$false -ErrorAction SilentlyContinue + New-NetIPAddress -InterfaceIndex $adapter.ifIndex -IPAddress '192.168.42.1' -PrefixLength 24 -DefaultGateway $PhoneIp | Out-Null + Set-DnsClientServerAddress -InterfaceIndex $adapter.ifIndex -ServerAddresses $PhoneIp, '8.8.8.8' +} else { + Write-Host 'Mode : DHCP (phone runs dnsmasq at 192.168.42.129)' + # Remove the old static 192.168.42.1 configuration before switching to DHCP. + Get-NetIPAddress -InterfaceIndex $adapter.ifIndex -AddressFamily IPv4 -ErrorAction SilentlyContinue | + Remove-NetIPAddress -Confirm:$false -ErrorAction SilentlyContinue + Get-NetRoute -InterfaceIndex $adapter.ifIndex -AddressFamily IPv4 -ErrorAction SilentlyContinue | + Remove-NetRoute -Confirm:$false -ErrorAction SilentlyContinue + Set-NetIPInterface -InterfaceIndex $adapter.ifIndex -Dhcp Enabled + Set-DnsClientServerAddress -InterfaceIndex $adapter.ifIndex -ResetServerAddresses -ErrorAction SilentlyContinue + ipconfig /renew "$($adapter.Name)" | Out-Null +} + +# Prefer the phone over Wi-Fi/Ethernet while it is connected. +Set-NetIPInterface -InterfaceIndex $adapter.ifIndex -InterfaceMetric 5 +Write-Host 'Interface metric: 5 (phone is preferred)' + +$reachable = $false +for ($i = 1; $i -le 30; $i++) { + if (Test-Connection -ComputerName $PhoneIp -Count 1 -Quiet -ErrorAction SilentlyContinue) { + $reachable = $true + break + } + Start-Sleep -Seconds 1 +} + +if ($reachable) { + Write-Host "Phone : reachable at $PhoneIp" +} else { + Write-Warning "Phone : $PhoneIp に到達できません。スマホのテザリングが ON か確認してください。" +} + +Write-Host '' +Get-NetIPConfiguration -InterfaceIndex $adapter.ifIndex | Format-List + +if ($reachable) { + try { + $r = Invoke-WebRequest -Uri 'http://example.com/' -UseBasicParsing -TimeoutSec 10 + Write-Host "Internet: HTTP $($r.StatusCode) (via the phone)" + } catch { + Write-Warning "Internet: 確認できませんでした: $($_.Exception.Message)" + Write-Warning 'スマホ側の APN / データ制限(bw_datalimit)を確認してください。' + } +} + +Write-Host '' +Write-Host 'Done. 以後は USB を挿してアプリがテザリングを ON にすれば自動で接続します。' |
