#!/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