aboutsummaryrefslogtreecommitdiff
path: root/usb-tether
blob: 1c127112132a65c34940500b482a35dfbc327904 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/sh
# 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
echo "Requesting DHCP lease via $DHCP_CMD ..."
sudo $DHCP_CMD "$IFACE"

# --- 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

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"

# --- 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