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