You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wifi 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env bash
  2. ask() {
  3. local opts=()
  4. if [[ "$1" == '-1' ]]; then
  5. opts+=( '-n1' )
  6. one=1
  7. shift
  8. fi
  9. if [[ "$1" == '-s' ]]; then
  10. opts+=( '-s' )
  11. shift
  12. fi
  13. read -e -r "${opts[@]}" -p "$* " ans
  14. if [[ $one == 1 ]]; then
  15. echo
  16. [[ ${ans,} == y ]]
  17. else
  18. echo "$ans"
  19. fi
  20. }
  21. has() {
  22. local verbose=false
  23. if [[ $1 == '-v' ]]; then
  24. verbose=true
  25. shift
  26. fi
  27. for c in "$@"; do c="${c%% *}"
  28. if ! command -v "$c" &> /dev/null; then
  29. [[ "$verbose" == true ]] && err "$c not found"
  30. return 1
  31. fi
  32. done
  33. }
  34. err() {
  35. echo -e "\e[31m$1\e[0m" >&2
  36. }
  37. info() {
  38. echo -e "\e[34m$1\e[0m" >&2
  39. }
  40. die() {
  41. [[ -n "$1" ]] && err "$1"
  42. exit 1
  43. }
  44. has -v nmcli fzf || die
  45. network=$(nmcli d wifi | fzf --header-lines=1 | sed -r 's/^\s*\*?\s*//; s/\s*Infra.*//')
  46. [[ -z "$network" ]] && exit
  47. password=$(ask -s 'Password:')
  48. echo
  49. connect=$(nmcli device wifi connect "$network" password "$password")
  50. if [[ "$connect" == *'successfully activated'* ]]; then
  51. info "successfully connected to $network"
  52. else
  53. err "failed to connect to $network"
  54. err "$connect"
  55. fi