Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- 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() {
- printf '\e[31m%s\e[0m\n' "$*" >&2
- }
- die() {
- (($# > 0)) && err "$*"
- exit 1
- }
- cleanup() {
- while [[ $(wifiState) == "connecting" ]]; do
- ((i++))
- ((i > 15)) && break
- sleep 1
- done
- if [[ $(wifiState) == "connected" ]]; then
- echo "wifi is connected!"
- else
- nmcli radio wifi off
- nmcli radio wifi on
- echo -e "Could NOT connect to wifi!\nTry again."
- exit 1
- fi
- }
- wifiState() { nmcli device status | grep '\swifi\s' | awk '{ print $3 }'; }
- has -v nmcli fzf || die "At least one dependency is missing. Exit script."
- trap 'cleanup' EXIT
- if [[ $(wifiState) == "unavailable" ]]; then
- nmcli radio wifi on
- sleep 1
- if [[ $(wifiState) == "unavailable" ]]; then
- nmcli radio wifi on
- sleep 2
- if [[ $(wifiState) == "unavailable" ]]; then
- echo -e "radio wifi could not be turned on!\nTry later again."
- exit 1
- fi
- fi
- echo "The radio wifi has been turned on."
- fi
- if [[ $(wifiState) == "disconnected" ]]; then
- echo "Be patient a few seconds..."
- sleep 5
- if [[ $(wifiState) == "disconnected" ]]; then
- nmcli device wifi list --rescan yes &> /dev/null
- network=$(nmcli device wifi | fzf --ansi --height=40% --reverse --cycle --inline-info --header-lines=1)
- if [[ -n $network ]]; then
- network=$(sed -r 's/^\s*\*?\s*//; s/\s*(Ad-Hoc|Infra).*//' <<< "$network")
- echo "connecting to \"${network}\"..."
- echo "$network"
- nmcli -a device wifi connect "$network"
- fi
- else
- true
- fi
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement