-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathirnm
executable file
·476 lines (409 loc) · 16.1 KB
/
irnm
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/usr/bin/env bash
####--------************--------####
# irnm #
# ----************---- -
# improved rofi network manager #
####--------************--------####
#
# Author: cherrynoize
# https://github.com/cherrynoize/irnm
#
# Originally based on rofi-network-manager
# by P3rf (https://gitlab.com/P3rf/rofi-network-manager)
#
# Default values
# (You should override default values in the .conf file, rather
# than manually editing this file)
Y_AXIS=0
X_AXIS=0
QRCODE_DIR="/tmp/"
WIDTH_FIX_MAIN=1
WIDTH_FIX_STATUS=10
PASSWORD_ENTER="if connection is stored,hit enter/esc."
# Wireless interfaces
wifi_nics=("$(nmcli device | awk '$2=="wifi" {print $1}')")
wifi_nics_vector=()
WLAN_INT=0
# Wired interfaces
eth_nics=("$(nmcli device | awk '$2=="ethernet" {print $1}')")
eth_nics_vector=()
# Signal settings
CHANGE_BARS=false
SIGNAL_STRENGTH_0="0"
SIGNAL_STRENGTH_1="1"
SIGNAL_STRENGTH_2="12"
SIGNAL_STRENGTH_3="123"
SIGNAL_STRENGTH_4="1234"
# Loading notification id
nid="7777"
# Menu options
opt_disc="Disconnect"
opt_scan="Rescan"
opt_status="Connection status"
opt_wifi_pwd="View connection details"
opt_add_network="Add a new network"
opt_add_network_manual="Manual"
opt_add_network_hidden="Hidden"
opt_wifi_on="Wi-Fi on"
opt_wifi_off="Wi-Fi off"
opt_eth_on="Ethernet on"
opt_eth_off="Ethernet off"
opt_eth_conn="Ethernet connecting..."
opt_eth_na="Ethernet unavailable"
opt_wifi_ix="Wi-Fi interface"
opt_restart="Restart network"
opt_qr_code="View connection QR code"
opt_advanced_options="Advanced options"
opt_editor="Open connection editor"
opt_vpn="VPN settings"
option_prefix=" "
option_suffix=""
nonselect="\0nonselectable\x1ftrue" # make option nonselectable
# Program values =========================*°||
# (You probably don't want to touch these!) \/
# find program name and location
program_name="$(basename "$0")" # current program filename
program_name="${program_name%.*}" # trim extension
program_dir="$(eval 'dirname "$(readlink -f "$0")"')"
rofi_dir="${XDG_CONFIG_HOME:-$HOME/.config}/rofi"
# derive config files
conf_file="$program_name.conf"
rasi_file="$program_name.rasi"
# Title for notifications
notif_title="$program_name"
function add_option () {
while [ -n "$1" ]; do
OPTIONS+="$option_prefix$1$option_suffix\n"
shift
done
}
function strip_option () {
sed -e 's/^'"$option_prefix"'//' -e 's/'"$option_suffix"'$//' <<< "$1"
}
function config () {
source_config () {
[ -f "$1" ] && . "$1"
}
source_config "$program_dir/$conf_file" || source_config "$rofi_dir/$program_name/$conf_file"
if [ -f "$program_dir/$rasi_file" ]; then
rasi_dir="$program_dir"
elif [ -f "$rofi_dir/$program_name/$rasi_file" ]; then
rasi_dir="$rofi_dir/$program_name"
elif [ -f "$rofi_dir/$rasi_file" ]; then
rasi_dir="$rofi_dir"
else
echo "error: file $rasi_file not found"
exit 1
fi
[ -n "$rofi_theme" ] &&
rofi_theme_cli_param="-theme"
}
function initialize () {
for i in "${wifi_nics[@]}"; do
wifi_nics_vector+=("$(nmcli -f general.product device show "$i" 2>/dev/null | awk '{print $2}')")
done
for i in "${eth_nics[@]}"; do
eth_nics_vector+=("$(nmcli -f general.product device show "$i" 2>/dev/null | awk '{print $2}')")
done
wireless_interface_state
ethernet_interface_state
}
function notification () {
if [ "$NOTIFICATIONS" = "on" ]; then
dunstify -u "${3:-normal}" -r "$nid" "$1" "$2"
fi
}
function wireless_interface_state () {
if [ ! "${#wifi_nics[@]}" = "0" ]; then
ACTIVE_SSID="$(nmcli device status | grep "^${wifi_nics[$WLAN_INT]}." | awk '{print $4;}')"
WIFI_CON_STATE="$(nmcli device status | grep "^${wifi_nics[$WLAN_INT]}." | awk '{print $3;}')"
if [ "$WIFI_CON_STATE" = "unavailable" ]; then
add_option "Wi-Fi Disabled" "$opt_wifi_on" "$opt_scan"
elif [ "$WIFI_CON_STATE" = "connected" ]; then
PROMPT=${wifi_nics_vector[$WLAN_INT]}[${wifi_nics[$WLAN_INT]}]
WIFI_LIST=$(nmcli --fields SSID,SECURITY,BARS device wifi list ifname "${wifi_nics[$WLAN_INT]}")
wifi_list
OPTIONS="$(head -n 1 <<< "$WIFI_LIST")$nonselect"
OPTIONS+="$(tail -n +2 <<< "$WIFI_LIST")\n" # skip first line
if [ "$ACTIVE_SSID" = "--" ]; then
add_option "$opt_scan" "$opt_add_network" "$opt_wifi_off"
else
add_option "$opt_scan" "$opt_disc" "$opt_add_network" "$opt_wifi_off"
fi
fi
fi
}
function ethernet_interface_state () {
if [ ! "${#eth_nics[@]}" = "0" ]; then
case "$WIRED_CON_STATE" in
"disconnected")
add_option "$opt_eth_on"
;;
"connected")
add_option "$opt_eth_off"
;;
"connecting")
add_option "$opt_eth_conn"
;;
"unavailable"|*)
add_option "$opt_eth_na$nonselect"
;;
esac
fi
}
function rofi_menu () {
if [ "${#wifi_nics[@]}" -gt "1" ]; then
add_option "$opt_wifi_ix"
fi
add_option "$opt_advanced_options"
if [ "$WIRED_CON_STATE" = "connected" ]; then
PROMPT="${wifi_nics_vector[$WLAN_INT]}[${eth_nics[$WLAN_INT]}]"
else
PROMPT="${wifi_nics_vector[$WLAN_INT]}[${wifi_nics[$WLAN_INT]}]"
fi
choice="$(echo -ne "$OPTIONS" | rofi_cmd "$OPTIONS" $WIDTH_FIX_MAIN "-a 0")"
SSID="$(echo "$choice" | sed "s/\s\{2,\}/\|/g" | awk -F "|" '{ print $1; }')"
menu
}
function rofi_cmd () {
if [ -z "$WIDTH" ]; then
if [ -n "$1" ]; then
WIDTH="$(echo -e "$1" | awk '{ print length; }' | sort -n | tail -1)"
fi
(( WIDTH += $2 ))
(( WIDTH /= 2 ))
WIDTH+="em"
fi
rofi -config "$rasi_dir/$rasi_file"\
-dmenu -i -location "$window_position" -yoffset "$Y_AXIS" -xoffset "$X_AXIS" "$3"\
$rofi_theme_cli_param "$rofi_theme"\
-theme-str 'window{width: '"$WIDTH"';}textbox-prompt-colon{str:"'"$PROMPT"':";}'"$4"''
}
function change_wireless_interface () {
if [ "${#wifi_nics[@]}" = "2" ]; then
WLAN_INT=$((WLAN_INT?0:1)) # flip value
else
LIST_WLAN_INT=""
for i in "${!wifi_nics[@]}"; do
LIST_WLAN_INT=("${LIST_WLAN_INT[@]}${wifi_nics_vector[$i]}[${wifi_nics[$i]}]\n")
done
LIST_WLAN_INT[-1]=${LIST_WLAN_INT[-1]:-2}
CHANGE_WLAN_INT=$(echo -e "${LIST_WLAN_INT[@]}" | rofi_cmd "${LIST_WLAN_INT[@]}" $WIDTH_FIX_STATUS)
for i in "${!wifi_nics[@]}"; do
if [ "$CHANGE_WLAN_INT" = "${wifi_nics_vector[$i]}[${wifi_nics[$i]}]" ]; then
WLAN_INT="$i"
break
fi
done
fi
wireless_interface_state && ethernet_interface_state
}
function scan () {
if [ "$WIFI_CON_STATE" = "unavailable" ]; then
change_wifi_state "Wi-Fi" "Enabling Wi-Fi connection" "on"
fi
notification "Wifi" "* Please, wait... Scanning"
WIFI_LIST="$(nmcli --fields SSID,SECURITY,BARS device wifi list ifname "${wifi_nics[$WLAN_INT]}" --rescan yes)"
wifi_list
wireless_interface_state && ethernet_interface_state
dunstify -C "$nid"
}
function wifi_list(){
WIFI_LIST="$(echo -e "$WIFI_LIST" | awk -F' +' '{ if (!seen[$1]++) print}' | awk '$1!="--" {print}' | awk '$1 !~ "^'"${ACTIVE_SSID}"'"' )"
[ "$ASCII_OUT" = "true" ] && WIFI_LIST="$(echo -e "$WIFI_LIST" | sed 's/\(..*\)\*\{4,4\}/\1▂▄▆█/g' | sed 's/\(..*\)\*\{3,3\}/\1▂▄▆_/g' | sed 's/\(..*\)\*\{2,2\}/\1▂▄__/g' | sed 's/\(..*\)\*\{1,1\}/\1▂___/g')"
[ "$CHANGE_BARS" = "true" ] && WIFI_LIST="$(echo -e "$WIFI_LIST" | sed 's/\(.*\)▂▄▆█/\1'$SIGNAL_STRENGTH_4'/' | sed 's/\(.*\)▂▄▆_/\1'$SIGNAL_STRENGTH_3'/' | sed 's/\(.*\)▂▄__/\1'$SIGNAL_STRENGTH_2'/' | sed 's/\(.*\)▂___/\1'$SIGNAL_STRENGTH_1'/' | sed 's/\(.*\)____/\1'$SIGNAL_STRENGTH_0'/')"
}
function change_wifi_state () {
notification "$1" "$2"
nmcli radio wifi "$3"
}
function change_wired_state () {
notification "$1" "$2"
nmcli device "$3" "$4"
}
function net_restart () {
notification "$1" "$2"
nmcli networking off && sleep 3 && nmcli networking on
}
function disconnect () {
ACTIVE_SSID=$(nmcli -t -f GENERAL.CONNECTION dev show "${wifi_nics[$WLAN_INT]}" | cut -d ':' -f2)
notification "$1" "You're now disconnected from Wi-Fi network '$ACTIVE_SSID'"
nmcli con down id "$ACTIVE_SSID"
}
function check_wifi_connected () {
if [ "$(nmcli device status | grep "^${wifi_nics[$WLAN_INT]}." | awk '{print $3}')" = "connected" ]; then
disconnect "Connection_Terminated"
fi
}
function connect () {
check_wifi_connected
local ssid="$1"
local password="$2"
notification "-t 0 Wi-Fi" "Connecting to $ssid"
if [ "$(nmcli dev wifi con "$ssid" password "$password" ifname "${wifi_nics[$WLAN_INT]}" | grep -c "successfully activated")" = "1" ]; then
notification "Connection_Established" "You're now connected to Wi-Fi network '$ssid'"
else
notification "Connection_Error" "Connection cannot be established"
fi
}
function password_prompt () {
PROMPT="Enter_Password" && PASS=$(echo "$PASSWORD_ENTER" | rofi_cmd "$PASSWORD_ENTER" 4 "-password")
}
function enter_ssid () {
PROMPT="Enter_SSID" && SSID=$(rofi_cmd "" 40)
}
function stored_connection () {
check_wifi_connected
local ssid="$1"
notification "-t 0 Wi-Fi" "Connecting to $ssid"
if [ "$(nmcli dev wifi con "$ssid" ifname "${wifi_nics[$WLAN_INT]}" | grep -c "successfully activated")" = "1" ]; then
notification "Connection_Established" "You're now connected to Wi-Fi network '$ssid'"
else
notification "Connection_Error" "Connection cannot be established"
fi
}
function ssid () {
enter_ssid
if [ -n "$SSID" ]; then
password_prompt && check_wifi_connected
if [ "$1" = "manual" ]; then
{ [ -n "$PASS" ] && [ ! "$PASS" = "$PASSWORD_ENTER" ] && connect "$SSID" "$PASS"; } || stored_connection "$SSID"
elif [ "$1" = "hidden" ]; then
[ -n "$PASS" ] && [ ! "$PASS" = "$PASSWORD_ENTER" ] && {
nmcli con add type wifi con-name "$SSID" ssid "$SSID" ifname "${wifi_nics[$WLAN_INT]}"
nmcli con modify "$SSID" wifi-sec.key-mgmt wpa-psk
nmcli con modify "$SSID" wifi-sec.psk "$PASS"
} || [ "$(nmcli -g NAME con show | grep -c "$SSID")" = "0" ] && nmcli con add type wifi con-name "$SSID" ssid "$SSID" ifname "${wifi_nics[$WLAN_INT]}"
notification "-t 0 Wifi" "Connecting to $SSID"
{ [ "$(nmcli con up id "$SSID" | grep -c "successfully activated")" = "1" ] && notification "Connection_Established" "You're now connected to Wi-Fi network '$SSID'"; } || notification "Connection_Error" "Connection can not be established"
fi
fi
}
function interface_status () {
local -n INTERFACES=$1 && local -n INTERFACES_vector=$2
for i in "${!INTERFACES[@]}"; do
CON_STATE=$(nmcli device status | grep "^${INTERFACES[$i]}." | awk '{print $3}')
INT_NAME=${INTERFACES_vector[$i]}[${INTERFACES[$i]}]
[ "$CON_STATE" = "connected" ] && STATUS="$INT_NAME:\n\t$(nmcli -t -f GENERAL.CONNECTION dev show "${INTERFACES[$i]}" | awk -F '[:]' '{print $2}') ~ $(nmcli -t -f IP4.ADDRESS dev show "${INTERFACES[$i]}" | awk -F '[:/]' '{print $2}')" || STATUS="$INT_NAME: ${CON_STATE^}"
echo -e "$STATUS"
done
}
function status () {
OPTIONS=""
if [ ! "${#eth_nics[@]}" = "0" ]; then
add_option "$(interface_status eth_nics eth_nics_vector)"
fi
if [ ! "${#wifi_nics[@]}" = "0" ]; then
add_option "$(interface_status wifi_nics wifi_nics_vector)"
fi
ACTIVE_VPN="$(nmcli -g NAME,TYPE con show --active | awk '/:vpn/' | sed 's/:vpn.*//g')"
if [ -n "$ACTIVE_VPN" ]; then
add_option "$ACTIVE_VPN[VPN]: $(nmcli -g ip4.address con show "$ACTIVE_VPN" | awk -F '[:/]' '{ print $1; }')"
fi
echo -ne "$OPTIONS" | rofi_cmd "$OPTIONS" "$WIDTH_FIX_STATUS" "" "mainbox{children:[listview];}"
}
# fetches, stores and returns requested wifi info
function get_wifi_info () {
nmcli dev wifi show-password | grep -oP '(?<='"$1"': ).*' | head -1
}
# show wifi password and ssid
function show_wifi_pwd () {
OPTIONS="SSID: $(get_wifi_info SSID)\nPassword: $(get_wifi_info Password)\n"
[ -x "$(command -v qrencode)" ] && add_option "$opt_qr_code"
choice="$(echo -ne "$OPTIONS" | rofi_cmd "$OPTIONS" $WIDTH_FIX_STATUS "-a -1" "mainbox{children:[listview];}")"
menu
}
# copy text to clipboard and send notification
function copy_to_clipboard () {
xclip -selection clipboard <<< "$1"
notification "$notif_title" "Copied to clipboard"
}
function gen_qrcode () {
DIRECTIONS=("Center" "Northwest" "North" "Northeast" "East" "Southeast" "South" "Southwest" "West")
TMP_SSID="${SSID// /_}"
qr_code_filename="$QRCODE_DIR$TMP_SSID.png"
[ -e "$qr_code_filename" ] || qrencode -t png -o "$qr_code_filename" -l H -s 25 -m 2 --dpi=192 "WIFI:S:""$SSID"";T:""$(get_wifi_info Security)"";P:""$(get_wifi_info Password)"";;"
rofi_cmd "" "0" ""\
"entry { enabled: false; }\
window {\
location:${DIRECTIONS[${qrcode_position:-"$window_position"}]};\
border-radius:6mm;padding:1mm;width:100mm;height:100mm;\
background-image: url(\"$qr_code_filename\", both);\
}"
}
function add_network () {
OPTIONS=""
add_option "Add network" "$opt_add_network_manual" "$opt_add_network_hidden"
choice="$(echo -ne "$OPTIONS" | rofi_cmd "$OPTIONS" "$WIDTH_FIX_STATUS" "" "mainbox{children:[listview];}")"
menu
}
function vpn () {
ACTIVE_VPN="$(nmcli -g NAME,TYPE con show --active | awk '/:vpn/' | sed 's/:vpn.*//g')"
opt_disable_vpn="Disable $ACTIVE_VPN"
if [ -n "$ACTIVE_VPN" ]; then
OPTIONS="$opt_disable_vpn"
else
OPTIONS="$(nmcli -g NAME,TYPE connection | awk '/:vpn/' | sed 's/:vpn.*//g')"
fi
VPN_ACTION=$(echo -ne "$OPTIONS" | rofi_cmd "$OPTIONS" "$WIDTH_FIX_STATUS" "" "mainbox {children:[listview];}")
if [ -n "$VPN_ACTION" ]; then
if [ "$VPN_ACTION" = "$opt_disable_vpn" ]; then
nmcli connection down "$ACTIVE_VPN" && notification "VPN_Deactivated" "$ACTIVE_VPN"
else
notification "-t 0 Activating_VPN" "$VPN_ACTION"
VPN_OUTPUT="$(nmcli connection up "$VPN_ACTION" 2>/dev/null)"
if grep -c "Connection successfully activated" <<< "$VPN_OUTPUT" > /dev/null; then
notification "VPN_Successfully_Activated" "$VPN_ACTION"
else
notification "Error_Activating_VPN" "Check your configuration for $VPN_ACTION"
fi
fi
fi
}
function advanced_options () {
OPTIONS=""
[ "$WIFI_CON_STATE" = "connected" ] && add_option "$opt_wifi_pwd"
add_option "$opt_status" "$opt_restart"
[ -n "$(nmcli -g NAME,TYPE connection | awk '/:vpn/' | sed 's/:vpn.*//g')" ] && add_option "$opt_vpn"
[ -x "$(command -v nm-connection-editor)" ] && add_option "$opt_editor"
choice="$(echo -ne "$OPTIONS" | rofi_cmd "$OPTIONS" "$WIDTH_FIX_STATUS" "" "mainbox {children:[listview];}")"
menu
}
function menu () {
case "$(strip_option "$choice")" in
"$opt_disc") disconnect "Connection_Terminated" ;;
"$opt_scan") scan; rofi_menu ;;
"$opt_status") status ;;
"$opt_wifi_pwd") show_wifi_pwd ;;
"$opt_add_network") add_network ;;
"$opt_add_network_manual") ssid manual ;;
"$opt_add_network_hidden") ssid hidden ;;
"$opt_wifi_on") change_wifi_state "Wi-Fi" "Enabling Wi-Fi connection" "on" ;;
"$opt_wifi_off") change_wifi_state "Wi-Fi" "Disabling Wi-Fi connection" "off" ;;
"$opt_eth_off") change_wired_state "Ethernet" "Disabling Wired connection" "disconnect" "${eth_nics[0]}" ;;
"$opt_eth_on") change_wired_state "Ethernet" "Enabling Wired connection" "connect" "${eth_nics[0]}" ;;
"$opt_wifi_ix") change_wireless_interface; rofi_menu ;;
"$opt_restart") net_restart "Network" "Restarting Network" ;;
"$opt_qr_code") gen_qrcode ;;
"$opt_advanced_options") advanced_options; rofi_menu ;;
"$opt_editor") nm-connection-editor ;;
"$opt_vpn") vpn ;;
SSID:*) copy_to_clipboard "$(get_wifi_info SSID)" ;;
Password:*) copy_to_clipboard "$(get_wifi_info Password)" ;;
*)
[ -n "$choice" ] && [[ "$WIFI_LIST" =~ .*"$choice".* ]] && { # if choice is in wifi list
[ "$SSID" = "*" ] && SSID="$(echo "$choice" | sed "s/\s\{2,\}/\|/g " | awk -F "|" '{print $3}')"
{ [ "$ACTIVE_SSID" = "$SSID" ] && nmcli con up "$SSID" ifname "${wifi_nics[$WLAN_INT]}"; } || {
security="$(awk '{print $2;}' <<< "$choice")"
[[ "$security" =~ "WPA".* ]] || [ "$security" = "WEP" ] && password_prompt
{ [ -n "$PASS" ] && [ ! "$PASS" = "$PASSWORD_ENTER" ] && connect "$SSID" "$PASS"; } || stored_connection "$SSID"
}
}
;;
esac
exit
}
config
notification "$notif_title" "* Please, wait..."
initialize && dunstify -C "$nid"
rofi_menu