-
Notifications
You must be signed in to change notification settings - Fork 1
/
wifi-connect
executable file
·63 lines (54 loc) · 1.52 KB
/
wifi-connect
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
#!/bin/bash
set -euo pipefail
# Read SSID
ssid=
if [ -z "${1+x}" ]; then
echo -n "SSID: " && read -r ssid
else
ssid="$1"
fi
# If the connection already exists, activate it.
if nmcli c | awk '{print $1}' | tail -n+2 | grep -E "^${ssid}$" > /dev/null; then
nmcli c up "${ssid}"
exit $?
fi
# Obtain network information in less than 1 minute
timeout=$(( $(date +%s) + 60 ))
info=
while true; do
info=$(nmcli dev wifi list | grep "${ssid}" || true)
if [[ -n "${info}" ]]; then
break
else
current_time=$(date +%s)
if [[ ${current_time} -gt ${timeout} ]]; then
echo "Couldn't obtain info for $ssid"
exit 1
else
sleep 5s
fi
fi
done
# Add connection
output=$(nmcli c add type "wifi" ifname "*" con-name "${ssid}" autoconnect "yes" save "yes" ssid "${ssid}")
exit_code=$?
if [[ $exit_code -eq 0 ]] ; then
uuid=$(echo "${output}" | grep -E -o '[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12}')
# Check security needed for that network
if [[ "${info}" =~ 802.1X ]]; then
# Read password
echo -n "IDENTITY: " && read -r identity
echo -n "PASSWORD: " && read -r -s pass
nmcli c modify "${uuid}" 802-1x.identity "${identity}" wifi-sec.key-mgmt wpa-eap 802-1x.eap peap 802-1x.phase2-auth mschapv2 802-1x.password "${pass}"
elif [[ "${info}" =~ WPA2 ]]; then
# Read password
echo -n "PASSWORD: " && read -r -s pass
nmcli c modify "${uuid}" wifi-sec.key-mgmt wpa-psk wifi-sec.psk "${pass}"
fi
# Activate connection
nmcli c up "${uuid}"
else
echo "${output}"
exit $exit_code
fi
exit 0