diff options
Diffstat (limited to 'pc/pc-autoconnect-linux.sh')
| -rw-r--r-- | pc/pc-autoconnect-linux.sh | 114 |
1 files changed, 114 insertions, 0 deletions
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 |
