1
1
Atdalīts 0
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

64 rindas
1.1 KiB

#!/usr/bin/env bash
ask() {
local opts=()
if [[ "$1" == '-1' ]]; then
opts+=( '-n1' )
one=1
shift
fi
if [[ "$1" == '-s' ]]; then
opts+=( '-s' )
shift
fi
read -e -r "${opts[@]}" -p "$* " ans
if [[ $one == 1 ]]; then
echo
[[ ${ans,} == y ]]
else
echo "$ans"
fi
}
has() {
local verbose=false
if [[ $1 == '-v' ]]; then
verbose=true
shift
fi
for c in "$@"; do c="${c%% *}"
if ! command -v "$c" &> /dev/null; then
[[ "$verbose" == true ]] && err "$c not found"
return 1
fi
done
}
err() {
echo -e "\e[31m$1\e[0m" >&2
}
info() {
echo -e "\e[34m$1\e[0m" >&2
}
die() {
[[ -n "$1" ]] && err "$1"
exit 1
}
has -v nmcli fzf || die
network=$(nmcli d wifi | fzf --header-lines=1 | sed -r 's/^\s*\*?\s*//; s/\s*Infra.*//')
[[ -z "$network" ]] && exit
password=$(ask -s 'Password:')
echo
connect=$(nmcli device wifi connect "$network" password "$password")
if [[ "$connect" == *'successfully activated'* ]]; then
info "successfully connected to $network"
else
err "failed to connect to $network"
err "$connect"
fi