From bdb38bb1424b61fa8a1d2b92c3545433ad165f68 Mon Sep 17 00:00:00 2001 From: Blake Romero Date: Thu, 28 Nov 2024 23:42:49 +0000 Subject: Add iwd control script --- iwd-control.sh | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 iwd-control.sh 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 + + -- cgit