-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpiaccess.sh
executable file
·170 lines (151 loc) · 5.4 KB
/
piaccess.sh
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
#!/bin/bash
# verify sudo
if [ $(id -u) -ne 0 ]; then
echo "Script must be run as root. Try sudo ./piaccess.sh"
exit 1
fi
if [ -d ${BASH_SOURCE%/*} ]; then
cd ${BASH_SOURCE%/*}
fi
# read config file
CFG_FILE=pistrap.conf
eval $(sed -r '/[^=]+=[^=]+/!d;s/\s+=\s+/=/g;s/\r//g' "$CFG_FILE")
# check if disable
if [ "$1" = "--disable" ]; then
# user confirmation
echo "Disable Raspberry Pi Access Point"
echo -ne "static ip:\t" && ( [ "$static_ip" = true ] && echo "yes" || echo "no (using DHCP)" )
if [ "$static_ip" = true ]; then
echo " interface $static_interface"
echo " static ip_address=$static_ip_address"
echo " static routers=$static_routers"
echo " static domain_name_servers=$static_dns"
fi
echo
read -p "Revert by using the above setting, do you want to continue [Y/n]? " -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
# stop related service
systemctl stop hostapd
systemctl stop dnsmasq
systemctl disable hostapd &>/dev/null
systemctl disable dnsmasq &>/dev/null
# revert ip forwarding and routing for security
sed -i '/net.ipv4.ip_forward=/c\net.ipv4.ip_forward=0' /etc/sysctl.conf
sed -i "/iptables -t nat -A POSTROUTING/d" /etc/rc.local
# revert ip and dhcp setting using pistrap.conf
cp raspbian_conf/dhcpcd.conf /etc/dhcpcd.conf
if [ "$static_ip" = true ]; then
echo "Configuring static ip address"
metric=100
for int in $static_interface; do
echo "interface $int" >> /etc/dhcpcd.conf
echo " metric $metric" >> /etc/dhcpcd.conf
echo " static ip_address=$static_ip_address" >> /etc/dhcpcd.conf
echo " static routers=$static_routers" >> /etc/dhcpcd.conf
echo " static domain_name_servers=$static_dns" >> /etc/dhcpcd.conf
echo "" >> /etc/dhcpcd.conf
metric=$((metric + 100))
done
fi
exit 0
fi
# user confirmation
echo "Enable Raspberry Pi Access Point"
echo -e "ssid:\t$ssid"
echo -e "psk:\t$psk"
echo ""
echo -e "internet_interface:\t\t\t$internet_interface"
echo -ne "static_ip_on_internet_interface:\t" && ( [ "$static_ip_on_internet_interface" = true ] && echo "yes" || echo "no (using DHCP)" )
if [ "$static_ip_on_internet_interface" = true ]; then
echo " interface $internet_interface"
echo " static ip_address=$static_ip_address"
echo " static routers=$static_routers"
echo " static domain_name_servers=$static_dns"
fi
echo ""
echo -e "ap_interface:\t\t$ap_interface"
echo -e "interface_ip:\t\t$interface_ip"
echo -e "number_of_client:\t$number_of_client"
echo -e "starting_ip:\t\t$starting_ip"
echo
read -p "Using the above setting, do you want to continue [Y/n]? " -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
# install prerequisite
if ! (dpkg -l | grep -q dnsmasq) || ! (dpkg -l | grep -q hostapd); then
echo "Installing dependency: dnsmasq hostapd"
apt-get update &>/dev/null
apt-get install -y dnsmasq hostapd &>/dev/null
fi
# configure wlan interface IP
cp raspbian_conf/dhcpcd.conf /etc/dhcpcd.conf
echo "interface $ap_interface" >> /etc/dhcpcd.conf
echo " static ip_address=$interface_ip/24" >> /etc/dhcpcd.conf
echo " nohook wpa_supplicant" >> /etc/dhcpcd.conf
echo " denyinterfaces $ap_interface" >> /etc/dhcpcd.conf
echo "" >> /etc/dhcpcd.conf
# configure static IP for Internet interface
if [ "$static_ip_on_internet_interface" = true ]; then
echo "interface $internet_interface" >> /etc/dhcpcd.conf
echo " static ip_address=$static_ip_address" >> /etc/dhcpcd.conf
echo " static routers=$static_routers" >> /etc/dhcpcd.conf
echo " static domain_name_servers=$static_dns" >> /etc/dhcpcd.conf
echo "" >> /etc/dhcpcd.conf
fi
# configure dnsmasq DHCP server
cp raspbian_conf/dnsmasq.conf /etc/dnsmasq.conf
echo "interface=$ap_interface" >> /etc/dnsmasq.conf
base_ip=`echo $interface_ip | cut -d"." -f1-3`
ending_ip=$base_ip.$((starting_ip+number_of_client))
starting_ip=$base_ip.$starting_ip
echo "dhcp-range=$starting_ip,$ending_ip,255.255.255.0,24h" >> /etc/dnsmasq.conf
# configure hostapd
cp raspbian_conf/hostapd /etc/default/hostapd
echo "
interface=$ap_interface
driver=nl80211
ssid=$ssid
hw_mode=g
channel=3
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=$psk
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
" > /etc/hostapd/hostapd.conf
# restart all service after configuration
echo "Configuration done, restarting dhcp service and hostapd service"
service dhcpcd restart &>/dev/null
systemctl restart hostapd
systemctl restart dnsmasq
systemctl enable hostapd &>/dev/null
systemctl enable dnsmasq &>/dev/null
systemctl daemon-reload
# setup IP packet forwarding and routing
sed -i '/net.ipv4.ip_forward=/c\net.ipv4.ip_forward=1' /etc/sysctl.conf
# iptables -t nat -A POSTROUTING -o $internet_interface -j MASQUERADE
# add rule above in rc.local
if grep -q 'iptables \-t nat \-A POSTROUTING' /etc/rc.local; then
sed -i "/iptables -t nat -A POSTROUTING/c\iptables -t nat -A POSTROUTING -o $internet_interface -j MASQUERADE" /etc/rc.local
else
sed -i "/^exit 0$/c\iptables -t nat -A POSTROUTING -o $internet_interface -j MASQUERADE\n\nexit 0" /etc/rc.local
fi
# done configuration, reboot
echo "Configuration done, most of the setting need reboot to be effective"
read -p "Reboot now [Y/n]? " -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
reboot
fi