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 | |
| download | tethering-app-main.tar.gz tethering-app-main.zip | |
Diffstat (limited to 'pc')
| -rw-r--r-- | pc/pc-autoconnect-freebsd.sh | 97 | ||||
| -rw-r--r-- | pc/pc-autoconnect-linux.sh | 114 | ||||
| -rw-r--r-- | pc/pc-autoconnect.bat | 5 | ||||
| -rw-r--r-- | pc/pc-autoconnect.ps1 | 115 | ||||
| -rw-r--r-- | pc/pc-autoconnect.sh | 22 |
5 files changed, 353 insertions, 0 deletions
diff --git a/pc/pc-autoconnect-freebsd.sh b/pc/pc-autoconnect-freebsd.sh new file mode 100644 index 0000000..2410305 --- /dev/null +++ b/pc/pc-autoconnect-freebsd.sh @@ -0,0 +1,97 @@ +#!/bin/sh +# 701KC USB tethering - FreeBSD host auto connect +# +# sudo ./pc-autoconnect-freebsd.sh # DHCP (recommended) +# sudo ./pc-autoconnect-freebsd.sh --static # static 192.168.42.1/24 +# sudo IFACE=ue0 ./pc-autoconnect-freebsd.sh # force interface +# +# The phone's RNDIS gadget usually appears as ue0 (if_urndis) or cdce0. + +set -eu + +PHONE_IP=192.168.42.129 +STATIC=0 +if [ "${1:-}" = "--static" ]; then + STATIC=1 +fi + +if [ "$(id -u)" -ne 0 ]; then + echo "root で実行してください: sudo $0 $*" >&2 + exit 1 +fi + +find_iface() { + if [ -n "${IFACE:-}" ]; then + echo "$IFACE" + return + fi + for iface in $(ifconfig -l); do + case "$iface" in + ue[0-9]*|cdce[0-9]*|ure[0-9]*) + echo "$iface" + return + ;; + esac + done +} + +IFACE=$(find_iface || true) +if [ -z "${IFACE:-}" ]; then + echo "RNDIS インターフェース (ue0 / cdce0 など) が見つかりません。" >&2 + echo "USB を挿してスマホ側のテザリングを ON にしてください。" >&2 + echo "手動指定: sudo IFACE=ue0 $0" >&2 + exit 1 +fi + +echo "Interface: $IFACE" +ifconfig "$IFACE" up + +if [ "$STATIC" -eq 1 ]; then + echo "Mode : static 192.168.42.1/24 via $PHONE_IP" + ifconfig "$IFACE" inet 192.168.42.1/24 + route add -net 192.168.42.0/24 -iface "$IFACE" 2>/dev/null || true + route add default "$PHONE_IP" 2>/dev/null || route change default "$PHONE_IP" 2>/dev/null || true +else + echo "Mode : DHCP" + if command -v dhclient >/dev/null 2>&1; then + dhclient "$IFACE" + elif command -v dhcpcd >/dev/null 2>&1; then + dhcpcd "$IFACE" + else + echo "dhclient / dhcpcd が見つかりません。--static を使ってください。" >&2 + exit 1 + fi +fi + +echo "Waiting for $PHONE_IP ..." +i=0 +while [ "$i" -lt 30 ]; do + if ping -c 1 -t 2 "$PHONE_IP" >/dev/null 2>&1; then + break + fi + i=$((i + 1)) + sleep 1 +done + +ifconfig "$IFACE" || true +netstat -rn | head -20 || true + +if ping -c 2 -t 2 "$PHONE_IP" >/dev/null 2>&1; then + echo "Phone : reachable at $PHONE_IP" +else + echo "Phone : unreachable (テザリングが ON か確認してください)" >&2 +fi + +if command -v curl >/dev/null 2>&1; then + if curl -fsS --max-time 10 http://example.com/ >/dev/null 2>&1; then + echo "Internet: OK" + else + echo "Internet: NG (APN / データ制限を確認)" >&2 + fi +elif command -v fetch >/dev/null 2>&1; then + if fetch -q -T 10 -o /dev/null http://example.com/ 2>/dev/null; then + echo "Internet: OK" + else + echo "Internet: NG (APN / データ制限を確認)" >&2 + fi +fi diff --git a/pc/pc-autoconnect-linux.sh b/pc/pc-autoconnect-linux.sh new file mode 100644 index 0000000..ffbaa26 --- /dev/null +++ b/pc/pc-autoconnect-linux.sh @@ -0,0 +1,114 @@ +#!/bin/sh +# 701KC USB tethering - Linux host auto connect +# +# sudo ./pc-autoconnect-linux.sh # DHCP (recommended) +# sudo ./pc-autoconnect-linux.sh --static # static 192.168.42.1/24 +# sudo IFACE=usb0 ./pc-autoconnect-linux.sh # force interface +# +# The phone runs dnsmasq on usb0 (192.168.42.129), so DHCP normally works. + +set -eu + +PHONE_IP=192.168.42.129 +STATIC=0 +if [ "${1:-}" = "--static" ]; then + STATIC=1 +fi + +if [ "$(id -u)" -ne 0 ]; then + echo "root で実行してください: sudo $0 $*" >&2 + exit 1 +fi + +find_iface() { + if [ -n "${IFACE:-}" ]; then + echo "$IFACE" + return + fi + for n in /sys/class/net/*; do + [ -e "$n" ] || continue + iface=${n##*/} + driver=$(readlink "$n/device/driver" 2>/dev/null | sed 's#.*/##') + case "$driver" in + rndis_host|cdc_ether|usbnet) + echo "$iface" + return + ;; + esac + done + for iface in usb0 ue0; do + [ -e "/sys/class/net/$iface" ] && { echo "$iface"; return; } + done +} + +IFACE=$(find_iface || true) +if [ -z "${IFACE:-}" ]; then + echo "RNDIS インターフェースが見つかりません。" >&2 + echo "USB を挿してスマホ側のテザリングを ON にしてください。" >&2 + echo "手動指定: sudo IFACE=usb0 $0" >&2 + exit 1 +fi + +echo "Interface: $IFACE" +ip link set "$IFACE" up + +if [ "$STATIC" -eq 1 ]; then + echo "Mode : static 192.168.42.1/24 via $PHONE_IP" + ip -4 addr flush dev "$IFACE" 2>/dev/null || true + ip addr add 192.168.42.1/24 dev "$IFACE" + ip route replace default via "$PHONE_IP" dev "$IFACE" metric 5 +else + echo "Mode : DHCP" + if command -v nmcli >/dev/null 2>&1 && [ "$(nmcli -t -f RUNNING general 2>/dev/null || true)" = "running" ]; then + con="tethering-701kc" + nmcli connection delete "$con" >/dev/null 2>&1 || true + nmcli connection add type ethernet ifname "$IFACE" con-name "$con" \ + ipv4.method auto ipv4.route-metric 5 connection.autoconnect yes >/dev/null + nmcli connection up "$con" >/dev/null + elif command -v dhcpcd >/dev/null 2>&1; then + dhcpcd -n "$IFACE" 2>/dev/null || dhcpcd "$IFACE" + elif command -v dhclient >/dev/null 2>&1; then + dhclient -v "$IFACE" + elif command -v udhcpc >/dev/null 2>&1; then + udhcpc -i "$IFACE" -q -n + else + echo "DHCP クライアントが見つかりません。--static を使ってください。" >&2 + exit 1 + fi +fi + +# Prefer the phone while it is connected. +ip route replace default via "$PHONE_IP" dev "$IFACE" metric 5 2>/dev/null || true + +echo "Waiting for $PHONE_IP ..." +i=0 +while [ "$i" -lt 30 ]; do + if ping -c 1 -W 2 "$PHONE_IP" >/dev/null 2>&1; then + break + fi + i=$((i + 1)) + sleep 1 +done + +ip -4 addr show "$IFACE" || true +ip route || true + +if ping -c 2 -W 2 "$PHONE_IP" >/dev/null 2>&1; then + echo "Phone : reachable at $PHONE_IP" +else + echo "Phone : unreachable (テザリングが ON か確認してください)" >&2 +fi + +if command -v curl >/dev/null 2>&1; then + if curl -fsS --max-time 10 http://example.com/ >/dev/null 2>&1; then + echo "Internet: OK" + else + echo "Internet: NG (APN / データ制限を確認)" >&2 + fi +elif command -v wget >/dev/null 2>&1; then + if wget -q -T 10 -O /dev/null http://example.com/ 2>/dev/null; then + echo "Internet: OK" + else + echo "Internet: NG (APN / データ制限を確認)" >&2 + fi +fi diff --git a/pc/pc-autoconnect.bat b/pc/pc-autoconnect.bat new file mode 100644 index 0000000..dd7fadc --- /dev/null +++ b/pc/pc-autoconnect.bat @@ -0,0 +1,5 @@ +@echo off +setlocal +powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0pc-autoconnect.ps1" %* +echo. +pause 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 にすれば自動で接続します。' diff --git a/pc/pc-autoconnect.sh b/pc/pc-autoconnect.sh new file mode 100644 index 0000000..5f2a224 --- /dev/null +++ b/pc/pc-autoconnect.sh @@ -0,0 +1,22 @@ +#!/bin/sh +# 701KC USB tethering - OS dispatcher +# +# sudo ./pc-autoconnect.sh # DHCP +# sudo ./pc-autoconnect.sh --static # static fallback +# +set -eu + +DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) + +case "$(uname -s)" in + Linux*) + exec "$DIR/pc-autoconnect-linux.sh" "$@" + ;; + FreeBSD*) + exec "$DIR/pc-autoconnect-freebsd.sh" "$@" + ;; + *) + echo "Unsupported OS: $(uname -s)" >&2 + exit 1 + ;; +esac |
