|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Starts a scan of available broadcasting SSIDs |
| 4 | +# nmcli dev wifi rescan |
| 5 | + |
| 6 | +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 7 | +SECURITY=0 |
| 8 | + |
| 9 | +if [ ! -r "$DIR/config" ]; then |
| 10 | + echo "WARNING: config file not found! Using default values." |
| 11 | + FIELDS=SSID,SECURITY,BARS |
| 12 | + ROFI_OPTS= |
| 13 | +elif [ -r "$DIR/config" ]; then |
| 14 | + # Get values from config |
| 15 | + source $DIR/config |
| 16 | +fi |
| 17 | + |
| 18 | +LIST=$(nmcli --fields "$FIELDS" device wifi list | sed '/^--/d') |
| 19 | +# For some reason rofi always approximates character width 2 short... hmmm |
| 20 | +RWIDTH=$(($(echo "$LIST" | head -n 1 | awk '{print length($0); }')+2)) |
| 21 | +# Dynamically change the height of the rofi menu |
| 22 | +LINENUM=$(echo "$LIST" | wc -l) |
| 23 | +let LINENUM+=2 |
| 24 | + |
| 25 | +# Gives a list of known connections so we can parse it later |
| 26 | +KNOWNCON=$(nmcli connection show) |
| 27 | +# telling if there is currently a connection (enabled or disabled) |
| 28 | +CONSTATE=$(nmcli r wifi) |
| 29 | + |
| 30 | +CURRSSID=$(iwgetid -r) |
| 31 | + |
| 32 | + |
| 33 | +if [[ ! -z $CURRSSID ]]; then |
| 34 | + HIGHLINE=$(echo "$(echo "$LIST" | awk -F "[ ]{2,}" '{print $1}' | grep -Fxn -m 1 "$CURRSSID" | awk -F ":" '{print $1}') + 1" | bc ) |
| 35 | +fi |
| 36 | + |
| 37 | +# HOPEFULLY you won't need this as often as I do |
| 38 | +# If there are more than 8 SSIDs, the menu will still only have 8 lines |
| 39 | +if [ "$LINENUM" -gt 8 ] && [[ "$CONSTATE" =~ "enabled" ]]; then |
| 40 | + LINENUM=8 |
| 41 | +elif [[ "$CONSTATE" =~ "disabled" ]]; then |
| 42 | + LINENUM=1 |
| 43 | +fi |
| 44 | + |
| 45 | +if [[ "$CONSTATE" =~ "enabled" ]]; then |
| 46 | + TOGGLE="toggle off" |
| 47 | +elif [[ "$CONSTATE" =~ "disabled" ]]; then |
| 48 | + TOGGLE="toggle on" |
| 49 | +fi |
| 50 | + |
| 51 | + |
| 52 | +CHENTRY=$(echo -e "$TOGGLE\nmanual\n$LIST" | uniq -u | rofi -dmenu -p "Wi-Fi SSID: " -lines "$LINENUM" -a "$HIGHLINE" $ROFI_OPTS -width -"$RWIDTH") |
| 53 | +CHSSID=$(echo "$CHENTRY" | sed 's/\s\{2,\}/\|/g' | awk -F "|" '{print $1}') |
| 54 | + |
| 55 | +if [[ -z $CHENTRY ]]; then |
| 56 | + exit; |
| 57 | +fi |
| 58 | + |
| 59 | +# If the user inputs "manual" as their SSID in the start window, it will bring them to this screen |
| 60 | +if [ "$CHENTRY" = "manual" ] ; then |
| 61 | + # Manual entry of the SSID and password (if appplicable) |
| 62 | + MSSID=$(echo "enter the SSID of the network (SSID,password)" | rofi -dmenu -p "Manual Entry: " -lines 1 $ROFI_OPTS) |
| 63 | + # Separating the password from the entered string |
| 64 | + MPASS=$(echo "$MSSID" | awk -F "," '{print $2}') |
| 65 | + |
| 66 | + if [[ -z $MSSID ]]; then |
| 67 | + exit; |
| 68 | + fi |
| 69 | + |
| 70 | + # FIXME: connexion logic duplicated, merge with bottom lines |
| 71 | + # If the user entered a manual password, then use the password nmcli command |
| 72 | + if [ "$MPASS" = "" ]; then |
| 73 | + nmcli dev wifi con "$MSSID" |
| 74 | + else |
| 75 | + nmcli dev wifi con "$MSSID" password "$MPASS" |
| 76 | + fi |
| 77 | + |
| 78 | +elif [ "$CHENTRY" = "toggle on" ]; then |
| 79 | + nmcli radio wifi on |
| 80 | + |
| 81 | +elif [ "$CHENTRY" = "toggle off" ]; then |
| 82 | + nmcli radio wifi off |
| 83 | + |
| 84 | +else |
| 85 | + |
| 86 | + # If the connection is already in use, then this will still be able to get the SSID |
| 87 | + if [ "$CHSSID" = "*" ]; then |
| 88 | + CHSSID=$(echo "$CHENTRY" | sed 's/\s\{2,\}/\|/g' | awk -F "|" '{print $3}') |
| 89 | + fi |
| 90 | + |
| 91 | + # Parses the list of preconfigured connections to see if it already contains the chosen SSID. This speeds up the connection process |
| 92 | + # FIXME: not always working |
| 93 | + if [[ $(echo "$KNOWNCON" | grep "$CHSSID") = "$CHSSID" ]]; then |
| 94 | + nmcli con up "$CHSSID" |
| 95 | + else |
| 96 | + if [[ "$CHENTRY" =~ "WPA1" ]] || [[ "$CHENTRY" =~ "WPA2" ]] || [[ "$CHENTRY" =~ "WEP" ]]; then |
| 97 | + SECURITY=1 |
| 98 | + WIFIPASS=$(rofi -dmenu -no-fixed-num-lines -p "password: " -mesg "if connection is stored, hit enter" $ROFI_OPTS) |
| 99 | + fi |
| 100 | + |
| 101 | + # FIXME: merge with previous code one fixed |
| 102 | + if [[ -z $WIFIPASS ]] && [[ $SECURITY ]]; then |
| 103 | + # network regitered already, force connection |
| 104 | + nmcli con up id "$CHSSID" |
| 105 | + else |
| 106 | + # connect and store the network |
| 107 | + nmcli dev wifi con "$CHSSID" password "$WIFIPASS" |
| 108 | + fi |
| 109 | + nmcli dev wifi con "$CHSSID" password "$WIFIPASS" |
| 110 | + fi |
| 111 | + |
| 112 | +fi |
0 commit comments