summaryrefslogtreecommitdiffstats
path: root/networking/wifi/device-files/connect.sh
blob: 133fbd5b05b17c96c13cf107e64842ec8eeb5055 (plain)
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
#!/system/bin/bash
#
# Copyright (C) 2017 Wolfgang Wiedmeyer <wolfgit@wiedmeyer.de>
# Copyright (C) 2016, 2017 Filippo Fil Bergamo
#
# This file is part of "RepWifi", a set of shell scripts to ease libre wifi connectivity.
#
# RepWifi is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RepWifi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# setup environment variables:
source "/data/misc/wifi/setup_variables.sh"
if [ $? -ne 0 ]; then
	exit 1
fi

# parse arguments <ssid> <password>
if [ $# -eq 0 ]; then
	echo "Missing arguments <ssid> <password> "
	exit 1
fi

if [ $# -ne 2 ]; then
	echo "Wrong argument count!"
	exit 1
fi

# Set SSID and password of your wifi access point
# WPA-Personal authentication method is assumed
SSID="$1"
PASSWORD="$2"

wpa_comm="wpa_cli -p$socketfile -P$pidfile -i$ifacename"

sup_pid=$(pidof wpa_supplicant)
dhcp_pid=$(pidof dhcpcd)

if ! [ -z "$sup_pid" ]; then
	killall -SIGINT wpa_supplicant
fi

if ! [ -z "$dhcp_pid" ]; then
	killall -SIGINT dhcpcd
fi

sleep 1s

ndc network destroy 1
ndc interface clearaddrs "$ifacename"

# seems like explicitly pulling up the interface is necessary..
# without this step, the interface gets removed during the execution of the script.
ifconfig "$ifacename" up

wpa_supplicant -B -dd -i"$ifacename" -C"$socketfile" -P"$pidfile" -I/system/etc/wifi/wpa_supplicant_overlay.conf -e/data/misc/wifi/entropy.bin

sleep 1s

if [ $? -ne 0 ]; then
	echo "[DEBUG]starting wpa_supplicant FAILED!"
	exit 1
fi

$wpa_comm add_network
if [ $? -ne 0 ]; then
	echo "[DEBUG]add_network FAILED!"
	exit 1
fi
$wpa_comm set_network 0 ssid '"'"$SSID"'"'
if [ $? -ne 0 ]; then
	echo "[DEBUG]set_network ssid FAILED!"
	exit 1
fi
$wpa_comm set_network 0 psk '"'"$PASSWORD"'"'
if [ $? -ne 0 ]; then
	echo "[DEBUG]set_network psk FAILED!"
	exit 1
fi
$wpa_comm select_network 0
if [ $? -ne 0 ]; then
	echo "[DEBUG]select_network 0 FAILED!"
	exit 1
fi
$wpa_comm enable_network 0
if [ $? -ne 0 ]; then
	echo "[DEBUG] enable_network 0 FAILED!"
	exit 1
fi
$wpa_comm reassociate
if [ $? -ne 0 ]; then
	echo "[DEBUG] reassociate FAILED!"
	exit 1
fi

# negotiate a dhcp leasing
dhcpcd "$ifacename"

# get gateway
gateway=$(ip route show 0.0.0.0/0 dev wlan0 | cut -d\  -f3)

ndc network create 1
ndc network interface add 1 "$ifacename"
ndc network route add 1 "$ifacename"  0.0.0.0/0 "$gateway"
ndc resolver setnetdns 1 "" "$ipDNS1" "$ipDNS2"
ndc network default set 1