-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi-tethering-bridge.sh
More file actions
65 lines (53 loc) · 2.11 KB
/
Copy pathpi-tethering-bridge.sh
File metadata and controls
65 lines (53 loc) · 2.11 KB
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
57
58
59
60
61
62
63
64
65
#!/bin/bash
# Configure a Raspberry Pi as a transparent bridge to interface a tethered
# phone (usb) with a routeur (ethernet). We then plug the Pi to the wan port of
# the router to share the internet access provided by the phone.
#
# This method offers couple advantages compared to ip forwarding. We don't
# need to create iptables rules, we don't need two differents subnets, and
# we also don't need to install and manage a dhcp server. All we have to do is
# to create the bridge and ask the dhcp server of the phone for an address.
#
# Also, we can use the phone as a trigger to control the bridge dynamically with
# the two options 'pre-up' and 'down'. So when the phone is plugged in, the
# bridge is automatically set up and vice versa.
#
# On the other hand, the main problem with this setup is the fact that if the
# phone is not connected, the Pi becomes unreachable from the local network. And
# even if the phone is connected, the IP of the bridge may change from time to time.
# In these conditions, trying to manage the Pi via ssh can quickly become a pain.
#
# One solution consists of using another network interface like a usb to ethernet
# adapter. That way, if something goes wrong we only have to plug the adapter to
# access the Pi again. And we can either give it a static IP or a permanent DHCP
# lease via the router's configuration.
if [[ $EUID -ne 0 ]]; then
echo "Run it as root"
exit
fi
# We could use 'iproute2' instead but 'brctl' offers a simpler syntax.
# https://unix.stackexchange.com/a/255489/284125
if ! command -v brctl &> /dev/null; then
echo "This script requires 'brctl'"
echo "Run 'apt install bridge-utils' to install it"
exit
fi
echo "Adding new rules to /etc/network/interfaces"
cat >> /etc/network/interfaces <<EOF
# Ethernet (built-in)
iface eth0 inet manual
# Ethernet (usb adapter)
allow-hotplug eth1
iface eth1 inet dhcp
# Tethering
allow-hotplug usb0
iface usb0 inet manual
pre-up ifup br0
down ifdown br0
# Bridge
iface br0 inet dhcp
bridge_ports eth0 usb0
EOF
echo "Disabling dhcpcd service"
systemctl disable dhcpcd.service
echo "Done, reboot to apply changes."