Advertisement
Guest User

wifi rofi script

a guest
Jul 21st, 2022
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.34 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. wifi_on='直'
  4. wifi_off='睊'
  5. wifi_disconnect='泌'
  6.  
  7. wifi_connected=''
  8.  
  9. wifi_lock=''
  10. wifi_unlock=''
  11.  
  12. signal_weak='◔'
  13. signal_fair='◑'
  14. signal_good='◕'
  15. signal_strong='●'
  16.  
  17. rofi_command="rofi -dmenu -no-fixed-num-lines \
  18.                   -yoffset -100 -i -p"
  19.  
  20. divider="━━━━━━━━━━━━━━━━━━━━━━━"
  21.  
  22. power_p() {
  23.     if nmcli --fields WIFI g | grep -q "enabled"; then
  24.         return 0
  25.     else
  26.         return 1
  27.     fi
  28. }
  29.  
  30. connected_p() {
  31.     local wifi=$(nmcli d | grep "wifi")
  32.     if echo $wifi | grep -q "disconnected"; then
  33.         return 1
  34.     else
  35.         return 0
  36.     fi
  37. }
  38.  
  39. connection_current(){
  40.     echo $(nmcli d | grep "wifi" | sed 's|.*connected||;s/ //g')
  41. }
  42.  
  43. power_toggle() {
  44.     if power_p; then
  45.         nmcli radio wifi off
  46.     else
  47.         nmcli radio wifi on
  48.     fi
  49. }
  50.  
  51. connections_get() {
  52.     nmcli -t d wifi rescan
  53.     local connection_list=$(
  54.         nmcli --fields SECURITY,SSID,BARS device wifi list |
  55.             #            sed '/^--/d' |
  56.             sed 1d |
  57.             sed "s/▂___/~$signal_weak/;s/▂▄__/~$signal_fair/;s/▂▄▆_/~$signal_good/;s/▂▄▆█/~$signal_strong/" |
  58.             sed -E "s/WPA*.?\S/$wifi_lock~/g" |
  59.             sed "s/$wifi_lock~ $wifi_lock~/$wifi_lock~/g;s/802\.1X//g;s/--/$wifi_unlock~/g;s/  *~/~/g;s/~  */~/g")
  60.    
  61.     if connected_p; then
  62.         local connection_current_=$(connection_current)
  63.  
  64.         local connection_new=$(
  65.             echo "$connection_list" |
  66.                 sed "s/$connection_current_/$wifi_connected $connection_current_ $wifi_connected/" |
  67.                 column -t -s '~')
  68.         echo "$connection_new"
  69.     else
  70.         local connection_new=$(
  71.             echo "$connection_list" |
  72.                 column -t -s '~')
  73.         echo "$connection_new"
  74.     fi
  75. }
  76.  
  77. ssid_get() {
  78.     local if_current=$(connection_current)
  79.     if [[ "$1" =~ "$if_current" ]]; then
  80.         echo "$if_current"
  81.         return 0
  82.     else
  83.         echo "$1" | awk -F "  " '{ print $2 }'
  84.         return 1
  85.     fi
  86. }
  87.  
  88. password_enter() {
  89.     rofi -dmenu\
  90.          -i\
  91.          -no-fixed-num-lines\
  92.          -p "Enter password"
  93. }
  94.  
  95. confirm() {
  96.     rofi -dmenu\
  97.          -i\
  98.          -no-fixed-num-lines\
  99.          -p "Are you sure? ((Y)es): "
  100. }
  101.  
  102. yes_p() {
  103.     local ans=$(confirm &)
  104.     if [[ $ans == "Y" || $ans == "y" || $ans == "Н" || $ans == "н" ]]; then
  105.         return 0
  106.     else
  107.         return 1
  108.     fi
  109. }
  110.  
  111. connection_menu() {
  112.  
  113.     local ssid=$(ssid_get "$1")
  114.  
  115.     if ssid_get "$1"; then
  116.         local options="Disconnect\nDelete\n$divider\nBack"
  117.     else
  118.         local options="Connect\n$divider\nBack"
  119.     fi
  120.  
  121.     local chosen="$(echo -e "$options" | $rofi_command "$ssid")"
  122.  
  123.     case $chosen in
  124.         "")
  125.             echo "No option chosen."
  126.             ;;
  127.         $divider)
  128.             connection_menu "$1"
  129.             ;;
  130.         "Connect")
  131.             if nmcli connection show | grep -q "$ssid"; then
  132.                 if nmcli con up "$ssid"; then
  133.                     notify-send "Connected to $ssid"
  134.                 else
  135.                     notify-send "Connection failed"
  136.                     connection_menu "$1"
  137.                 fi
  138.             elif [[ "$1" =~ "$wifi_lock" ]]; then
  139.                 local password=$(password_enter)
  140.                 if nmcli dev wifi con "$ssid" password "$password"; then
  141.                     notify-send "Connected to $ssid"
  142.                 else
  143.                     notify-send "Connection failed"
  144.                     connection_menu "$1"
  145.                 fi
  146.             else
  147.                 if nmcli dev wifi con "$ssid"; then
  148.                     notify-send "Connected to $ssid"
  149.                 else
  150.                     notify-send "Connection failed"
  151.                     connection_menu "$1"
  152.                 fi
  153.             fi
  154.             ;;
  155.         "Disconnect")
  156.             nmcli con down "$ssid"
  157.             notify-send "Disconnected"
  158.             ;;
  159.         "Delete")
  160.             if yes_p; then
  161.                 nmcli connection delete id "$ssid"
  162.                 notify-send "Connection $ssid deleted"
  163.             else
  164.                 connection_menu "$1"
  165.             fi
  166.             ;;
  167.         "Back")
  168.             main_menu
  169.             ;;
  170.     esac
  171. }
  172.  
  173. main_menu() {
  174.  
  175.     if power_p; then
  176.         local power="WIFI: on"
  177.         local list=$(connections_get)
  178.        
  179.         local options="$list\n$divider\n$power\nExit"
  180.     else
  181.         local power="WIFI: off"
  182.  
  183.         local options="$power\nExit"
  184.     fi
  185.  
  186.     local chosen="$(echo -e "$options" | $rofi_command "WiFi")"
  187.  
  188.     case $chosen in
  189.         "")
  190.             echo "No option chosen."
  191.             ;;
  192.         "Exit")
  193.             exit 0
  194.             ;;
  195.         $divider)
  196.             main_menu
  197.             ;;
  198.         $power)
  199.             power_toggle
  200.             ;;
  201.         *)
  202.             if [[ $chosen ]] ; then connection_menu "$chosen"; fi
  203.             ;;
  204.     esac
  205. }
  206.  
  207. print_status() {
  208.     if power_p; then
  209.         if connected_p; then
  210.             echo $wifi_on
  211.         else
  212.             echo $wifi_disconnect
  213.         fi
  214.     else
  215.         echo $wifi_off
  216.     fi  
  217. }
  218.  
  219. case "$1" in
  220.     --status)
  221.         print_status
  222.         ;;
  223.     --toggle)
  224.         power_toggle
  225.         ;;
  226.     *)
  227.         main_menu
  228.         ;;
  229. esac
  230.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement