aboutsummaryrefslogtreecommitdiff
path: root/usb-tether
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukas@labunix.xyz>2026-05-19 18:38:39 +0200
committerLukasz Kasprzak <lukas@labunix.xyz>2026-05-19 18:38:39 +0200
commite8cf42f2f17151378574ee5b50f29904a1067bee (patch)
tree86b4db80971ecf9818ad769bd8b7fa337d218731 /usb-tether
parentc3bfa44d1b6da88c184fc3312944cf47d1dfd7f2 (diff)
downloadbin-main.tar.gz
bin-main.zip
added readmeHEADmain
Diffstat (limited to 'usb-tether')
-rwxr-xr-xusb-tether55
1 files changed, 49 insertions, 6 deletions
diff --git a/usb-tether b/usb-tether
index bbe16ed..1c12711 100755
--- a/usb-tether
+++ b/usb-tether
@@ -1,13 +1,56 @@
#!/bin/sh
-IFACE=$(ip -br a | grep 'enx' | awk '{print $1}')
+# USB tethering activator for Devuan
+# Brings up a USB-tethered interface, gets DHCP, and routes default traffic through it.
+set -eu
+
+# --- 1. Find the USB tethering interface ---
+IFACE=$(ip -br link | awk '/^(enx|usb|rndis)/ && /UP/ {print $1; exit}')
+if [ -z "$IFACE" ]; then
+ echo "Error: no USB tethering interface (enx*/usb*/rndis*) found that is UP." >&2
+ echo "Make sure the phone is plugged in and USB tethering is enabled." >&2
+ exit 1
+fi
+echo "Found USB interface: $IFACE"
+
+# --- 2. Pick a DHCP client ---
+if command -v dhcpcd >/dev/null 2>&1; then
+ DHCP_CMD="dhcpcd -q"
+elif command -v dhclient >/dev/null 2>&1; then
+ DHCP_CMD="dhclient -v"
+else
+ echo "Error: neither dhcpcd nor dhclient is installed." >&2
+ echo "Install one: sudo apt install dhcpcd5 (or) sudo apt install isc-dhcp-client" >&2
+ exit 1
+fi
+
+# --- 3. Bring the link up and request a lease ---
sudo ip link set "$IFACE" up
-sudo dhcpcd "$IFACE"
+echo "Requesting DHCP lease via $DHCP_CMD ..."
+sudo $DHCP_CMD "$IFACE"
-GATEWAY=$(ip route | grep "$IFACE" | awk '/default/ {print $3}')
+# --- 4. Wait for the default route to populate ---
+GATEWAY=""
+for i in 1 2 3 4 5 6 7 8 9 10; do
+ GATEWAY=$(ip route show dev "$IFACE" | awk '/default/ {print $3; exit}')
+ [ -n "$GATEWAY" ] && break
+ sleep 1
+done
-sudo ip route del default
-sudo ip route add default via "$GATEWAY" dev "$IFACE"
+if [ -z "$GATEWAY" ]; then
+ echo "Error: could not determine gateway on $IFACE after 10s." >&2
+ ip addr show "$IFACE" >&2
+ exit 1
+fi
+echo "Gateway: $GATEWAY"
-echo "USB tethering active on $IFACE"
+# --- 5. Replace the default route, preferring this interface ---
+# `ip route replace` is atomic — no need to delete first, and it won't fail
+# if there's no existing default. We use a low metric so this route wins.
+sudo ip route replace default via "$GATEWAY" dev "$IFACE" metric 50
+# --- 6. Confirm ---
+echo ""
+echo "USB tethering active on $IFACE"
+echo "Current default routes:"
+ip route show default