forked from lukicdarkoo/rpi-wifi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure
executable file
·195 lines (164 loc) · 5.08 KB
/
configure
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/bash
# The script configures simultaneous AP and Managed Mode Wifi on Raspberry Pi Zero W (should also work on Raspberry Pi 3)
# Licence: GPLv3
# Author: Darko Lukic <[email protected]>
# Special thanks to: https://albeec13.github.io/2017/09/26/raspberry-pi-zero-w-simultaneous-ap-and-managed-mode-wifi/
# Special thanks to: https://raspberrypi.stackexchange.com/questions/89803/access-point-as-wifi-router-repeater-optional-with-bridge/118877#118877
# Error management
set -o errexit
set -o pipefail
set -o nounset
usage() {
cat 1>&2 <<EOF
Configures simultaneous AP and Managed Mode Wifi on Raspberry Pi
USAGE:
rpi-wifi -a <ap_ssid> [<ap_password>] -c <client_ssid> [<client_password>]
rpi-wifi -a MyAP myappass -c MyWifiSSID mywifipass
PARAMETERS:
-a, --ap AP SSID & password
-c, --client Client SSID & password
-i, --ip AP IP
FLAGS:
-n, --no-internet Disable IP forwarding
-h, --help Show this help
EOF
exit 0
}
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-c|--client)
CLIENT_SSID="$2"
CLIENT_PASSPHRASE="$3"
shift
shift
shift
;;
-a|--ap)
AP_SSID="$2"
AP_PASSPHRASE="$3"
shift
shift
shift
;;
-i|--ip)
ARG_AP_IP="$2"
shift
shift
;;
-h|--help)
usage
shift
;;
-n|--no-internet)
NO_INTERNET="true"
shift
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL[@]}"
[ $AP_SSID ] || usage
AP_IP=${ARG_AP_IP:-'192.168.10.1'}
AP_IP_BEGIN=`echo "${AP_IP}" | sed -e 's/\.[0-9]\{1,3\}$//g'`
COUNTRY="$(wpa_cli -i wlan0 get country)"
# Install dependencies
sudo apt -y update
sudo apt -y upgrade
sudo apt -y install dnsmasq hostapd
# Exclude ap0 from `/etc/dhcpcd.conf`
sudo bash -c 'cat >> /etc/dhcpcd.conf' << EOF
# this defines static addressing to ap@wlan0 and disables wpa_supplicant for this interface
interface ap@wlan0
static ip_address=${AP_IP}/24
ipv4only
nohook wpa_supplicant
EOF
# Populate `/etc/dnsmasq.conf`
sudo bash -c 'cat >> /etc/dnsmasq.conf' << EOF
interface=lo,ap@wlan0
no-dhcp-interface=lo,wlan0
bind-interfaces
server=1.1.1.1
domain-needed
bogus-priv
dhcp-range=${AP_IP_BEGIN}.50,${AP_IP_BEGIN}.150,12h
dhcp-option=3,${AP_IP}
address=/soundbox.lan/${AP_IP}
EOF
# Populate `/etc/hostapd/hostapd.conf`
sudo bash -c 'test -f /etc/hostapd/hostapd.conf || cat > /etc/hostapd/hostapd.conf' << EOF
ctrl_interface=/var/run/hostapd
ctrl_interface_group=0
country_code=${COUNTRY:-US}
interface=ap@wlan0
driver=nl80211
ssid=${AP_SSID}
hw_mode=g
channel=11
wmm_enabled=0
macaddr_acl=0
auth_algs=1
wpa=2
$([ $AP_PASSPHRASE ] && echo "wpa_passphrase=${AP_PASSPHRASE}")
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP CCMP
rsn_pairwise=CCMP
EOF
sudo chmod 600 /etc/hostapd/hostapd.conf
sudo bash -c 'SYSTEMD_EDITOR=tee systemctl edit --force --full [email protected]' <<\EOF
[Unit]
Description=IEEE 802.11 ap@%i AP on %i with hostapd
Wants=wpa_supplicant@%i.service
[Service]
Type=forking
PIDFile=/run/hostapd.pid
Restart=on-failure
RestartSec=2
#Environment=DAEMON_CONF=/etc/hostapd/hostapd.conf
EnvironmentFile=-/etc/default/hostapd
ExecStartPre=/sbin/iw dev %i interface add ap@%i type __ap
ExecStart=/usr/sbin/hostapd -i ap@%i -P /run/hostapd.pid -B /etc/hostapd/hostapd.conf
ExecStopPost=-/sbin/iw dev ap@%i del
[Install]
WantedBy=sys-subsystem-net-devices-%i.device
EOF
sudo systemctl stop hostapd # if the default hostapd service was active before
sudo systemctl disable hostapd # if the default hostapd service was enabled before
sudo systemctl enable [email protected]
sudo rfkill unblock wlan
# Populate `/etc/wpa_supplicant/wpa_supplicant.conf`
sudo bash -c 'test -f /etc/wpa_supplicant/wpa_supplicant.conf || cat > /etc/wpa_supplicant/wpa_supplicant.conf' << EOF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=${COUNTRY:-US}
network={
ssid="${CLIENT_SSID}"
$([ $CLIENT_PASSPHRASE ] && echo "psk=\"${CLIENT_PASSPHRASE}\"")
}
EOF
sudo chmod 600 /etc/wpa_supplicant/wpa_supplicant.conf
# not used, as the agent is hooked by dhcpcd
sudo systemctl disable wpa_supplicant.service
# We can then follow Raspberry’s documentation to enable routing and IP masquerading:
sudo DEBIAN_FRONTEND=noninteractive apt install -y netfilter-persistent iptables-persistent
sudo bash -c 'test -f /etc/sysctl.d/routed-ap.conf || cat >/etc/sysctl.d/routed-ap.conf' <<\EOF
# https://www.raspberrypi.org/documentation/configuration/wireless/access-point-routed.md
# Enable IPv4 routing
net.ipv4.ip_forward=1
EOF
# Add firewall rules
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
sudo iptables -A FORWARD -i wlan0 -o ap@wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i ap@wlan0 -o wlan0 -j ACCEPT
sudo netfilter-persistent save
# persist powermanagement off for wlan0
grep 'iw dev wlan0 set power_save off' /etc/rc.local || sudo sed -i 's:^exit 0:iw dev wlan0 set power_save off\n\nexit 0:' /etc/rc.local
# Finish
echo "Wifi configuration is finished! Please reboot your Raspberry Pi to apply changes..."