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.

97 lines
3.1 KiB

  1. #!/usr/bin/env bash
  2. #DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  3. #DIR=${HOME}
  4. #echo $DIR
  5. FIELDS=SSID,SECURITY
  6. POSITION=0
  7. YOFF=0
  8. XOFF=0
  9. FONT="DejaVu Sans Mono 8"
  10. if [ -r "$HOME/.config/rofi/wifi" ]; then
  11. source "$HOME/.config/rofi/wifi"
  12. else
  13. echo "WARNING: config file not found! Using default values."
  14. fi
  15. LIST=$(nmcli --fields "$FIELDS" device wifi list | sed '/^--/d')
  16. # For some reason rofi always approximates character width 2 short... hmmm
  17. RWIDTH=$(($(echo "$LIST" | head -n 1 | awk '{print length($0); }')+2))
  18. # Dynamically change the height of the rofi menu
  19. LINENUM=$(echo "$LIST" | wc -l)
  20. # Gives a list of known connections so we can parse it later
  21. KNOWNCON=$(nmcli connection show)
  22. # Really janky way of telling if there is currently a connection
  23. CONSTATE=$(nmcli -fields WIFI g)
  24. CURRSSID=$(LANGUAGE=C nmcli -t -f active,ssid dev wifi | awk -F: '$1 ~ /^yes/ {print $2}')
  25. if [[ ! -z $CURRSSID ]]; then
  26. HIGHLINE=$(echo "$(echo "$LIST" | awk -F "[ ]{2,}" '{print $1}' | grep -Fxn -m 1 "$CURRSSID" | awk -F ":" '{print $1}') + 1" | bc )
  27. fi
  28. # HOPEFULLY you won't need this as often as I do
  29. # If there are more than 8 SSIDs, the menu will still only have 8 lines
  30. if [ "$LINENUM" -gt 8 ] && [[ "$CONSTATE" =~ "enabled" ]]; then
  31. LINENUM=8
  32. elif [[ "$CONSTATE" =~ "disabled" ]]; then
  33. LINENUM=1
  34. fi
  35. if [[ "$CONSTATE" =~ "enabled" ]]; then
  36. TOGGLE="toggle off"
  37. elif [[ "$CONSTATE" =~ "disabled" ]]; then
  38. TOGGLE="toggle on"
  39. fi
  40. CHENTRY=$(echo -e "$TOGGLE\nmanual\n$LIST" | uniq -u | rofi -dmenu -p "Wi-Fi SSID: " -lines "$LINENUM" -a "$HIGHLINE" -location "$POSITION" -yoffset "$YOFF" -xoffset "$XOFF" -font "$FONT" -width -"$RWIDTH")
  41. #echo "$CHENTRY"
  42. CHSSID=$(echo "$CHENTRY" | sed 's/\s\{2,\}/\|/g' | awk -F "|" '{print $1}')
  43. #echo "$CHSSID"
  44. # If the user inputs "manual" as their SSID in the start window, it will bring them to this screen
  45. if [ "$CHENTRY" = "manual" ] ; then
  46. # Manual entry of the SSID and password (if appplicable)
  47. MSSID=$(echo "enter the SSID of the network (SSID,password)" | rofi -dmenu -p "Manual Entry: " -font "$FONT" -lines 1)
  48. # Separating the password from the entered string
  49. MPASS=$(echo "$MSSID" | awk -F "," '{print $2}')
  50. #echo "$MSSID"
  51. #echo "$MPASS"
  52. # If the user entered a manual password, then use the password nmcli command
  53. if [ "$MPASS" = "" ]; then
  54. nmcli dev wifi con "$MSSID"
  55. else
  56. nmcli dev wifi con "$MSSID" password "$MPASS"
  57. fi
  58. elif [ "$CHENTRY" = "toggle on" ]; then
  59. nmcli radio wifi on
  60. elif [ "$CHENTRY" = "toggle off" ]; then
  61. nmcli radio wifi off
  62. else
  63. # If the connection is already in use, then this will still be able to get the SSID
  64. if [ "$CHSSID" = "*" ]; then
  65. CHSSID=$(echo "$CHENTRY" | sed 's/\s\{2,\}/\|/g' | awk -F "|" '{print $3}')
  66. fi
  67. # Parses the list of preconfigured connections to see if it already contains the chosen SSID. This speeds up the connection process
  68. if [[ $(echo "$KNOWNCON" | grep "$CHSSID") = "$CHSSID" ]]; then
  69. nmcli con up "$CHSSID"
  70. else
  71. if [[ "$CHENTRY" =~ "WPA2" ]] || [[ "$CHENTRY" =~ "WEP" ]]; then
  72. WIFIPASS=$(echo "if connection is stored, hit enter" | rofi -dmenu -p "password: " -lines 1 -font "$FONT" )
  73. fi
  74. nmcli dev wifi con "$CHSSID" password "$WIFIPASS"
  75. fi
  76. fi