aboutsummaryrefslogtreecommitdiff
path: root/wifi_connect
blob: a92a07df41b1378a731008f3086bff92fdda7bed (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
#!/usr/bin/env bash

# wpa_supplicant script to discover and add access points
# or connect to known access points

set -e

W_IF="wlan0"  # change with your right interface
WPA_CONF="/etc/wpa_supplicant/wpa_supplicant.conf"

# bogus call of doas to force auth so as to not be bothered by fzf blocking
# the prompt later on.
# assumes that you're not being prompt for passwd at every single call :)
doas echo -n

# with a little help from my friends
get_essid ()
{
	ESSID=$(doas iw ${W_IF} scan | rg "\tSSID" | \
	sed -e 's/^.*SSID: //g' | sort | uniq | fzf )
}

known_essid ()
{
	ID=$(doas wpa_cli list_networks | rg -F "${ESSID}")
}

connect_essid ()
{
	echo "${ESSID} found, connecting..."
	echo
	doas wpa_cli enable_network "${ID}"
	# uncomment following if using with wpa-conf
	#echo
	#doas dhclient -v ${W_IF}
}

add_essid ()
{
	while true; do
		echo "ESSID PASSWORD: "
    	read -r ESSID_PASSWD
		while true; do
			echo "Is \"${ESSID_PASSWD}\" correct? (y/n)"
			read -r ANSWER_PASSWD
			if [ "${ANSWER_PASSWD}" = "y" ]; then
				ENTRY="$(wpa_passphrase "$ESSID" "$ESSID_PASSWD")"
				echo "$ENTRY" | doas tee -a $WPA_CONF
			break
			fi
		done
		break
	done
	echo "${ESSID} added, connecting..."
}

get_essid
if known_essid; then
	connect_essid
	exit
else
	while true; do
		echo "Add this access point? (y/n)"
		read -r ANSWER_ADD
		if [ "${ANSWER_ADD}" = "y" ]; then
			add_essid
			wpa_cli reconfigure
			known_essid
			connect_essid
			exit
		fi
	done
fi