This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnmcli-rofi
executable file
·184 lines (148 loc) · 5.23 KB
/
nmcli-rofi
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# default config
FIELDS="SSID,SECURITY,BARS"
# supported locales (en, ru, de, fr, hi, ja)
declare -A LOC_ENABLE=(["en"]="enabled" ["ru"]="включен" ["de"]="aktiviert" ["fr"]="activé" ["hi"]="सक्षम" ["ja"]="有効")
declare -A LOC_ACTIVE=(["en"]="yes" ["de"]="ja")
declare -A LOC_INACTIVE=(["en"]="no" ["de"]="nein")
# get current locale
CURRLOCALE=$(locale | grep 'LANG=[a-z]*' -o | sed 's/^LANG=//g')
# 'enabled' in currnet locale
ENABLED="${LOC_ENABLE["$CURRLOCALE"]}"
ACTIVE="${LOC_ACTIVE["$CURRLOCALE"]}"
INACTIVE="${LOC_INACTIVE["$CURRLOCALE"]}"
# get current uuid
CURRUUID="$(nmcli -f UUID,TYPE con show --active | grep wifi | awk '{print $1}')"
# get wifi state
function wifistate () {
echo "$(nmcli -fields WIFI g | sed -n 2p)"
}
# get active wifi connections
function wifiactive () {
echo "$(nmcli con show --active | grep wifi)"
}
function if_wifistate () {
# return a expression based on wifi state
[[ "$(wifistate)" =~ $ENABLED ]] && rt=$1 || rt=$2
echo $rt
}
function toggle_wifi () {
toggle=$(if_wifistate "Disable Network" "Enable Network")
echo $toggle
}
function current_connection () {
currssid=$(iwgetid -r)
[[ "$currssid" != '' ]] && currcon="Disconnect from \"$currssid\"" || currcon=""
echo $currcon
}
function wifi_list () {
# get list of available connections without the active connection (if it's connected)
nmcli --fields IN-USE,"$FIELDS" device wifi list | sed "s/^IN-USE\s//g" | awk '{ if ($1 != "*") { print } }' | sed 's/^ *//g'
}
function vpn_list () {
nmcli --fields ACTIVE,NAME,TYPE con | awk '{ if ($NF == "vpn") { print "VPN: "$0 } }' | sed 's/ *vpn *$/"/g' | \
sed "s/ *$INACTIVE */ Connect to \"/g" | sed "s/ *$ACTIVE */ Disconnect from \"/g"
}
function menu () {
wa=$(wifiactive); ws=$(wifistate);
if [[ $ws =~ $ENABLED ]]; then
if [[ "$wa" != '' ]]; then
echo "$1\n\n$4\n\n$2\n$3\nManual Connection"
else
echo "$1\n\n$4\n\n$3\nManual Connection"
fi
else
echo "$4\n\n$3"
fi
}
function rofi_cmd () {
# don't repeat lines with uniq -u
echo -e "$1" | uniq -u | wofi --show dmenu --insensitive -p "Network connection" --style "$DIR/wofi-font.css" --cache /dev/null
}
function rofi_menu () {
TOGGLE=$(toggle_wifi)
CURRCONNECT=$(current_connection)
[[ "$TOGGLE" =~ 'Enable' ]] && WIFILIST="" || WIFILIST=$(wifi_list)
VPNLIST="$(vpn_list)"
MENU=$(menu "$WIFILIST" "$CURRCONNECT" "$TOGGLE" "$VPNLIST")
rofi_cmd "$MENU"
}
function get_ssid () {
# get fields in order
eval FIELDSARR=( $( echo "$FIELDS" | sed 's/,/ /g' ) )
# get position of SSID field
for i in "${!FIELDSARR[@]}"; do
if [[ "${FIELDSARR[$i]}" = "SSID" ]]; then
SSID_POS="${i}";
fi
done
# let for arithmetical vars
let AWKSSIDPOS=$SSID_POS+1
# get SSID from AWKSSIDPOS
CHSSID=$(echo "$1" | sed 's/\s\{2,\}/\|/g' | awk -F "|" '{print $'$AWKSSIDPOS'}')
echo "$CHSSID"
}
function cleanup_networks () {
nmcli --fields UUID,TIMESTAMP-REAL,DEVICE con show | grep -e '--' | awk '{print $1}' \
| while read line; do nmcli con delete uuid $line; done
}
function main () {
OPS=$(rofi_menu)
CHSSID=$(get_ssid "$OPS")
if [ -z "$OPS" ]; then
exit 0
fi
if [[ "$OPS" =~ 'Disable' ]]; then
nmcli radio wifi off
elif [[ "$OPS" =~ 'Enable' ]]; then
nmcli radio wifi on
elif [[ "$OPS" == "VPN: "* ]]; then
name=$(echo "$OPS" | cut -d'"' -f 2)
if [[ "$OPS" =~ "Connect" ]]; then
echo "Connect VPN $name"
nmcli connection up "$name"
else
echo "Disconnect VPN $name"
nmcli connection down "$name"
fi
elif [[ "$OPS" =~ 'Disconnect' ]]; then
nmcli con down uuid $CURRUUID
elif [[ "$OPS" =~ 'Manual' ]]; then
# Manual entry of the SSID
MSSID=$(echo -en "" | rofi -dmenu -p "SSID" -mesg "Enter the SSID of the network" \
-lines 0 -font "$FONT")
# manual entry of the PASSWORD
MPASS=$(echo -en "" | rofi -dmenu -password -p "PASSWORD" -mesg \
"Enter the PASSWORD of the network" -lines 0 -font "$FONT")
# If the user entered a manual password, then use the password nmcli command
if [ "$MPASS" = "" ]; then
nmcli dev wifi con "$MSSID"
elif [ "$MSSID" != '' ] && [ "$MPASS" != '' ]; then
nmcli dev wifi con "$MSSID" password "$MPASS"
fi
else
CHSSID=$(get_ssid "$OPS")
# Check if password exists
if nmcli connection | grep -q "$CHSSID"; then
nmcli connection up "$CHSSID"
else
if [[ "$OPS" =~ "WPA2" ]] || [[ "$OPS" =~ "WEP" ]]; then
WIFIPASS=$(echo -en "" | wofi --show dmenu --password -p "PASSWORD" --lines=0)
if [ -z "$WIFIPASS" ]; then
exit 0
fi
fi
if [[ "$CHSSID" != '' ]]; then
if [[ "$WIFIPASS" != '' ]]; then
nmcli dev wifi con "$CHSSID" password "$WIFIPASS"
else
nmcli dev wifi con "$CHSSID"
fi
fi
fi
fi
}
# clean up obsoleted connections
# nmcli --fields UUID,TIMESTAMP-REAL,DEVICE con show | grep never | awk '{print $1}' | while read line; do nmcli con delete uuid $line; done
main