wifimenu 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env bash
  2. DIVIDER="---------"
  3. RESCAN_BUTTON="Rescan"
  4. BACK="Back"
  5. # Load NetworkManager
  6. network_status=$(ps aux | grep NetworkManager | grep root)
  7. # If the service is not running
  8. if [ "$network_status" = "" ]; then
  9. rofi -dmenu -password -p "Root Password: " | sudo -S NetworkManager
  10. fi
  11. # Starts a scan of available broadcasting SSIDs
  12. notify-send "Getting list of available Wi-Fi networks..."
  13. # Get active connection
  14. active_network=$(nmcli device status | awk '{print $4}' | sed "/--/d" | sed "/(externally/d" | sed "/CONNECTION/d")
  15. if [ "$active_network" = "" ]; then
  16. active_network="Not Found"
  17. fi
  18. wifi_list=$(nmcli --fields "SECURITY,SSID" device wifi list | sed 1d | sed 's/ */ /g' | sed -E "s/WPA*.?\S/ /g" | sed "s/^--/ /g" | sed "s/ //g" | sed "/--/d")
  19. # Gives a list of known connections so we can parse it later
  20. connected=$(nmcli -fields WIFI g)
  21. if [[ "$connected" =~ "enabled" ]]; then
  22. toggle="Disable Wi-Fi"
  23. elif [[ "$connected" =~ "disabled" ]]; then
  24. toggle="Enable Wi-Fi"
  25. fi
  26. choose_options="$wifi_list\n$DIVIDER\n$toggle\n$RESCAN_BUTTON"
  27. # If wifi is disabled, then the options change
  28. if [ "$wifi_list" = "" ]; then
  29. choose_options="$toggle"
  30. fi
  31. chosen_network=$(echo -e "$choose_options" | uniq -u | rofi -dmenu -i -selected-row 0 -p "$active_network" )
  32. chosen_id=$(echo "${chosen_network:3}" | xargs)
  33. # Parses the list of preconfigured connections to see if it already contains the chosen SSID. This speeds up the connection process
  34. if [ "$chosen_network" = "" ]; then
  35. exit
  36. elif [ "$chosen_network" = "Enable Wi-Fi" ]; then
  37. nmcli radio wifi on
  38. elif [ "$chosen_network" = "Disable Wi-Fi" ]; then
  39. nmcli radio wifi off
  40. elif [ "$chosen_network" = "Rescan" ]; then
  41. notify-send "Device rescan started..."
  42. nmcli radio wifi off && nmcli radio wifi on && sleep 3 && sh "$HOME/bin/wifimenu"
  43. else
  44. # Message to show when connection is activated successfully
  45. success_message="You are now connected to the Wi-Fi network \"$chosen_id\"."
  46. # Get known connections
  47. saved_connections=$(nmcli -g NAME connection)
  48. if [[ $(echo "$saved_connections" | grep -w "$chosen_id") = "$chosen_id" ]]; then
  49. # Display information about the currently selected device
  50. modified_id=$(echo "$chosen_id" | tr -d '[:space:]' | awk '{print tolower($0)}')
  51. connection_info=$(nmcli --fields "SSID,BSSID,MODE,FREQ,RATE,SECURITY" device wifi list | sed "s/$chosen_id/$modified_id/" | awk -v chosen_id="$modified_id" '$1 == chosen_id {print "BS:", $2, "\nMODE:", $3, "\nFREQ:", $4, $5, "\nRATE:", $6, $7, "\nSECURITY:", $8}')
  52. # Processing logic in connection information
  53. wifi_details_options="$connection_info\n$DIVIDER\nChange password\nForget access point\nShare connection\n$BACK"
  54. chosen="$(echo -e "$wifi_details_options" | uniq -u | rofi -dmenu -i -selected-row 0 -p "$chosen_id")"
  55. echo $chosen
  56. case "$chosen" in
  57. "" | "$DIVIDER")
  58. echo "No option chosen."
  59. ;;
  60. "Change password")
  61. new_password=$(rofi -dmenu -password -p "Password: ")
  62. nmcli con modify "$chosen_id" wifi-sec.psk "$new_password"
  63. notify-send "The password for point '$chosen_id' has been changed."
  64. ;;
  65. "Forget access point")
  66. nmcli connection delete $chosen_id
  67. notify-send "The '$chosen_id' access point was forgotten."
  68. ;;
  69. "Back")
  70. sh "$HOME/bin/wifimenu"
  71. ;;
  72. "Share connection")
  73. alacritty -e sh -c 'nmcli dev wifi show-password; read -n 1 -s -r -p "Press any key to exit"'
  74. ;;
  75. *)
  76. echo $chosen | xclip -selection clipboard
  77. ;;
  78. esac
  79. nmcli connection up id "$chosen_id" | grep "successfully" && notify-send "Connection Established" "$success_message"
  80. else
  81. if [[ "$chosen_network" =~ "" ]]; then
  82. wifi_password=$(rofi -dmenu -password -p "Password: " )
  83. fi
  84. nmcli device wifi connect "$chosen_id" password "$wifi_password" | grep "successfully" && notify-send "Connection Established" "$success_message"
  85. fi
  86. fi