summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlake Romero <blake@blkrom.com>2024-11-28 23:42:49 +0000
committerBlake Romero <blake@blkrom.com>2024-11-28 23:43:15 +0000
commitbdb38bb1424b61fa8a1d2b92c3545433ad165f68 (patch)
tree76683730026c0de00cd5fbe5a58ea0e61fe88b3f
parent6eca4fcae7a42a1368b11e469330143884449e15 (diff)
Add iwd control script
-rwxr-xr-xiwd-control.sh70
1 files changed, 70 insertions, 0 deletions
diff --git a/iwd-control.sh b/iwd-control.sh
new file mode 100755
index 0000000..daef965
--- /dev/null
+++ b/iwd-control.sh
@@ -0,0 +1,70 @@
+#!/bin/sh
+#
+menu=bemenu
+device="wlan0"
+
+get_networks(){
+ 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
+
+