summaryrefslogtreecommitdiff
path: root/iwd-control.sh
blob: d4305297f61f63060a10ca93d601fbc31ee67711 (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
#!/bin/sh
#
menu=bemenu
device="wlan0"

get_networks(){
	iwctl device "$device" set-property Powered on > /dev/null 2>&1
	iwctl station "$device" scan > /dev/null 2>&1
	iwctl station "$device" get-networks > /dev/null 2>&1
	iwctl station "$device" get-networks \
		| awk 'NR>4' \
		| sed "$(printf 's/\033\[[0-9;]*m//g; s/^\W*//; /^$/d')" \
		| awk 'NF=(NF-2)'
}

confirm() {
	case $(printf "Yes\nNo" | $menu -l 2 -p "$1 ") in
		"Yes") return 0 ;;
		*) return 1 ;;
	esac
}

# Set device if unset
if [ -z "$device" ]; then
	devices=$(iwctl device list | awk 'NR>4{print$2}' | sed '/^$/d')
	device=$(printf "%s" "$devices" | $menu -p "Select device:")
	[ -z "$device" ] && exit 1
fi

while true; do
	# List actions
	state=$(iwctl station "$device" show | awk '/State/{print $2}')
	actions="Connect\n"
	[ "$state" = "connected" ] && actions="${actions}Disconnect\n"
	actions="${actions}Networks\n"

	choice=$(printf "$actions" | $menu)
	case "$choice" in
		"Connect")
			# Select a station
			ssid=$(printf "%s" "$(get_networks)" | $menu -p "Connect to:")
			[ -z "$ssid" ] && exit 1

			# Check if station is a known network
			if iwctl known-networks list | grep "$ssid" > /dev/null 2>&1; then
				iwctl station "$device" connect "$ssid" > /dev/null 2>&1
			else
				# If station not known, enter password
				password=$(echo | $menu -x indicator -l 0 -p "WIFI Password:")
				[ -z "$password" ] && exit 1
				iwctl --passphrase="$password" station "$device" connect "$ssid" > /dev/null 2>&1
				unset -v password
			fi
			exit
		;;
		"Disconnect")
			# Prompt to disconnect
			ssid=$(iwctl station "$device" show | awk '/Connected network/{print$3}')
			confirm "Disconnect ${ssid}?" && iwctl station "$device" disconnect
		;;
		"Networks")
			# Scan for networks
			printf "%s" "$(get_networks)" | $menu -p "Availble Networks:"
			# TODO: list station information
		;;
	*) exit
		;;
	esac
done