-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi-modem-router.sh
More file actions
executable file
·94 lines (71 loc) · 2.75 KB
/
Copy pathpi-modem-router.sh
File metadata and controls
executable file
·94 lines (71 loc) · 2.75 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
#
# Tested on Raspbian Jessie, Stretch and Buster (lite)
#
# The main purpose of this script is to forward the connection
# between the two interfaces wlan0 and eth0 to turn the Pi into a
# a modem-router. Where wlan0 acts as the 'wan' port.
# It also installs dnsmasq as a dhcp server for the ethernet port.
#
# For more infos and use cases, see the project 'hotspot-connect'
# https://github.com/Oxmel/hotspot-connect
# For some reason, network interfaces on raspbian buster still use the
# old naming standards (e.g: wlan0, eth0,...). Which doesn't follow
# the new interface naming system implemented recently on debian.
wlan_name="wlan0"
eth_name="eth0"
# Check if user has root privileges
if [[ $EUID -ne 0 ]]; then
echo "Run it as root"
exit
fi
# Check if internet is reachable
echo "Connectivity check..."
wget -q --tries=5 --timeout=10 --spider https://google.com > /dev/null
if [[ $? -eq 0 ]]; then
echo "Passed"
else
echo "Failed!"
exit
fi
# Only hide standard output, errors will still be displayed (if any)
echo "Installing dnsmasq..."
apt-get install dnsmasq -y > /dev/null && echo "Done" || { echo "Failed!"; exit; }
# Set up dhcp range for ethernet interface
# Current config allows 5 machines max to be addressed via dhcp
echo "Creating config for dnsmasq"
cat > /etc/dnsmasq.conf <<EOF
interface=$eth_name
dhcp-range=10.0.0.2,10.0.0.7,255.255.255.0,12h
EOF
echo "Configuring interfaces"
# Prevent dhcpcd from addressing eth0
echo "denyinterfaces $eth_name" >> /etc/dhcpcd.conf
# If dhcpcd detects that the main 'interfaces' file has been altered, it will
# stop managing network interfaces. But we still need it to handle dhcp
# requests on wlan0 so we write the custom config for eth0 in the folder
# '/etc/network/interfaces.d' instead.
cat >> /etc/network/interfaces.d/eth0-router <<EOF
# Provide a static ip for ethernet iface
allow-hotplug $eth_name
iface $eth_name inet static
address 10.0.0.1
netmask 255.255.255.0
network 10.0.0.0
broadcast 10.0.0.255
# Apply iptables rules once interfaces are up
post-up /etc/network/if-up.d/net-forward.sh
EOF
echo "Creating routing rules"
cat > /etc/network/if-up.d/net-forward.sh <<EOF
# Route wlan connection to ethernet subnet
iptables -t nat -A POSTROUTING -o $wlan_name -s 10.0.0.0/24 -j MASQUERADE
# Forward connection between those two interfaces
iptables -A FORWARD -i $wlan_name -o $eth_name -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i $eth_name -o $wlan_name -j ACCEPT
EOF
chmod +x /etc/network/if-up.d/net-forward.sh
echo "Activating ipv4 forwarding"
sed -i 's/#net.ipv4.ip_forward/net.ipv4.ip_forward/g' /etc/sysctl.conf
sed -i 's/net.ipv4.ip_forward=0/net.ipv4.ip_forward=1/g' /etc/sysctl.conf
echo "Done (reboot to apply changes)"