dots

git clone git://mattcarlson.org/repos/dots.git
Log | Files | Refs

commit 5a9b51f76e210215851a91c7c99632a5ff6f68f0
parent e319782a57045bf97c046b2e5e5b8d507f4fb034
Author: Matthew Carlson <matt@mcarlson.xyz>
Date:   Fri,  6 Aug 2021 16:06:52 -0400

more work on net script

Diffstat:
M.local/bin/bar/net.sh | 116++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
1 file changed, 80 insertions(+), 36 deletions(-)

diff --git a/.local/bin/bar/net.sh b/.local/bin/bar/net.sh @@ -12,52 +12,35 @@ net_menu() { [ -e "${dev}/wireless" ] && printf '%s\n' "${dev##*/}" done | head -n 1)" - # scan interface for available networks + # scan interface iwctl station "${interface}" scan + # get available networks and known networks + get_network_info "iwctl station "${interface}" get-networks" 'iwctl known-networks list' + # user can connect, disconnect, or forget network # don't care bout access points or other stuff show_menu 'Wifi Menu' 'Connect' 'Disconnect' 'Forget' } connect() { - # get networks and their types + signals - info="$(iwctl station "${interface}" get-networks | grep '\s' | tail +3 | awk '{ $1 = $1 }; 1' | sed -e 's/\x1b\[[0-9;]*m//g')" - - # names - network_names="$(get_network_info '1')" - # valid network types are open, wep, psk, 8021x - network_types="$(printf '%s\n' "${info}" | awk '{ for (i=1;i<=NF;i++){ if ($i ~ /open|wep|psk|8021x/) { print $i } } }')" - # strength will be 1-4 asterisks - network_strengths="$(get_network_info '2')" - - # combine info with delimiter b/c wifi names can have spaces which messes with awk - i=1 - combined_info="$(printf '%s\n' "${network_names}" | ( while read -r name; do - # get type and strength associated with network - ntype="$(printf '%s\n' "${network_types}" | sed -n ${i}p)" - strength="$(printf '%s\n' "${network_strengths}" | sed -n ${i}p)" - - # append network as new line to variable (with newline if not first) - if [ ${i} -eq 1 ]; then combined_info="${name}|${ntype}|${strength}" - else combined_info="${combined_info}${NEWLINE}${name}|${ntype}|${strength}" - fi - - i=$((i+1)) - done - printf '%s\n' "${combined_info}" ))" - # user can choose network to connect to - network="$(show_menu 'Connect' "${network_names}")" + network="$(show_menu 'Connect' "${available_network_names}")" # if network starts with '> ' it means user is already on that network case "${network}" in '> '*) printf 'Already connected to this network\n' && return ;; esac + # if network is known connect with no passphrase + if [ $(printf '%s\n' "${known_network_info}" | awk -F '|' '{print $1}' | grep "\<${network}\>") ]; then + iwctl station "${interface}" connect "${network}" + return + fi + # if network is wep, psk, or 8021x get passphrase - case "$(printf '%s\n' "${combined_info}" | grep "${network}" | awk -F '|' '{print $2}')" in - 'wep'|'psk'|'8021x') get_passphrase ;; + case "$(printf '%s\n' "${available_network_info}" | grep "\<${network}\>" | awk -F '|' '{print $2}')" in + 'wep'|'psk'|'8021x') get_passphrase ;; # if open just connect without passphrase - 'open') iwctl station "${interface}" connect "${network}" + 'open') iwctl station "${interface}" connect "${network}" ;; esac # connect with passphrase @@ -65,19 +48,80 @@ net_menu() { } disconnect() { - : + connected_network="$(iwctl station "${interface}" show | grep '\<Connected network\>' | awk '{ $1=""; $2=""; sub(" ", " "); {$1=$1;print} }')" + if [ -n "${connected_network}" ]; then + answer="$(show_menu "Disconnect from ${connected_network}?" 'Yes' 'No')" + case "$(printf '%s\n' "${answer}" | tr '[:upper:]' '[:lower:]')" in + 'yes') iwctl station "${interface}" disconnect ;; + 'no') return ;; + esac + fi } forget() { - : + # if there are known networks + if [ -n "${known_network_names}" ]; then + # get network + network="$(show_menu 'Forget' "${known_network_names}")" + + # get confirmation + answer="$(show_menu "Forget ${network}?" 'Yes' 'No')" + + # forget if answer is yes + [ "${answer}" = 'Yes' ] && iwctl known-networks "${network}" forget + fi } # get network columns separately - get_network_info() { + get_network_column() { types='open|wep|psk|8021x' printf '%s\n' "${info}" | awk -v var="${1}" -F "\\${types}" '{ print $var }' | sed 's/^[ \t]*//;s/[ \t]*$//' } + get_network_info() { + if [ "${1}" = "iwctl station "${interface}" get-networks" ]; then + info="$(eval "${1}" | grep '\s' | tail +3 | awk '{ $1 = $1 }; 1' | sed -e 's/\x1b\[[0-9;]*m//g')" + + available_network_names="$(get_network_column '1')" + available_network_types="$(printf '%s\n' "${info}" | awk '{ for (i=1;i<=NF;i++){ if ($i ~ /open|wep|psk|8021x/) { print $i } } }')" + available_network_strengths="$(get_network_column '2')" + + i=1 + available_network_info="$(printf '%s\n' "${available_network_names}" | ( while read -r name; do + type_field="$(printf '%s\n' "${available_network_types}" | sed -n ${i}p)" + strength_field="$(printf '%s\n' "${available_network_strengths}" | sed -n ${i}p)" + + if [ ${i} -eq 1 ]; then available_network_info="${name}|${type_field}|${strength_field}" + else available_network_info="${available_network_info}${NEWLINE}${name}|${type_field}|${strength_field}" + fi + + i=$((i+1)) + done + printf '%s\n' "${available_network_info}" ))" + fi + + if [ "${2}" = 'iwctl known-networks list' ]; then + info="$(eval "${2}" | grep '\s' | tail +3 | awk '{ $1 = $1 }; 1' | sed -e 's/\x1b\[[0-9;]*m//g')" + + known_network_names="$(get_network_column '1')" + known_network_types="$(printf '%s\n' "${info}" | awk '{ for (i=1;i<=NF;i++){ if ($i ~ /open|wep|psk|8021x/) { print $i } } }')" + known_network_dates="$(get_network_column '2')" + + i=1 + known_network_info="$(printf '%s\n' "${known_network_names}" | ( while read -r name; do + type_field="$(printf '%s\n' "${known_network_types}" | sed -n ${i}p)" + date_field="$(printf '%s\n' "${known_network_dates}" | sed -n ${i}p)" + + if [ ${i} -eq 1 ]; then known_network_info="${name}|${type_field}|${date_field}" + else known_network_info="${known_network_info}${NEWLINE}${name}|${type_field}|${date_field}" + fi + + i=$((i+1)) + done + printf '%s\n' "${known_network_info}" ))" + fi + } + # hide input with dmenu -P flag get_passphrase() { passphrase="$(dmenu -c -p 'Passphrase:' -P)" ; } @@ -97,9 +141,9 @@ net_menu() { # get user's answer printf '%s\n' "${options}" | while read -r line; do if [ "${line}" = "${answer}" ]; then - # if answer is a command run it else just print it + # if answer is a function run it else just print it cmd="$(printf '%s\n' "${answer}" | tr '[:upper:]' '[:lower:]')" - if command -v "${cmd}" 2>/dev/null; then "${cmd}" + if command -v "${cmd}" >/dev/null 2>&1 && [ "${cmd}" != 'yes' ]; then "${cmd}" else printf '%s\n' "${answer}" fi break