2019-07-18 16:45:46 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
#
|
|
|
|
# nmcli connection switching from a rofi menu.
|
|
|
|
# When a network is selected its status is switched from UP to DOWN or vice versa.
|
|
|
|
#
|
|
|
|
# WARNING, connection name can't contain a # or | symbol
|
|
|
|
#
|
|
|
|
|
|
|
|
ROFI="rofi -dmenu
|
|
|
|
-sep #
|
|
|
|
-i -p Network:
|
2020-07-03 18:08:19 +00:00
|
|
|
-theme $ROFI_THEME
|
2019-07-18 16:45:46 +00:00
|
|
|
-location 3
|
|
|
|
-yoffset +45
|
|
|
|
-xoffset -80
|
|
|
|
-width 30
|
|
|
|
"
|
|
|
|
|
2019-11-17 16:29:34 +00:00
|
|
|
build_rofi_menu()
|
2019-07-18 16:45:46 +00:00
|
|
|
{
|
|
|
|
while read -r line
|
|
|
|
do
|
2019-11-17 16:29:34 +00:00
|
|
|
name="$(echo "$line" | cut -d':' -f1)"
|
|
|
|
dtype="$(echo "$line" | cut -d':' -f2)"
|
|
|
|
device="$(echo "$line" | cut -d':' -f3)"
|
2019-07-18 16:45:46 +00:00
|
|
|
|
2019-11-17 16:29:34 +00:00
|
|
|
[ -z "$device" ] && status= || status=
|
2019-07-18 16:45:46 +00:00
|
|
|
dtype=${dtype##*-}
|
2019-11-17 16:29:34 +00:00
|
|
|
option=$(printf '%-35s | %-15s | %s\n' "$name" "$dtype" $status)
|
2019-08-07 14:44:17 +00:00
|
|
|
|
2019-07-18 16:45:46 +00:00
|
|
|
menu="$menu # $option"
|
|
|
|
done < <(nmcli -c no --terse -f NAME,TYPE,DEVICE connection show)
|
|
|
|
|
2019-11-17 16:29:34 +00:00
|
|
|
echo "$menu" | cut -c 3-
|
2019-07-18 16:45:46 +00:00
|
|
|
}
|
|
|
|
|
2019-11-17 16:29:34 +00:00
|
|
|
switch_connections()
|
2019-07-18 16:45:46 +00:00
|
|
|
{
|
2019-11-17 16:29:34 +00:00
|
|
|
name="$1"
|
|
|
|
status=$2
|
2019-07-18 16:45:46 +00:00
|
|
|
|
2019-11-17 16:29:34 +00:00
|
|
|
if [ "$status" = "" ]; then
|
2019-07-18 16:45:46 +00:00
|
|
|
nmcli connection down "$name"
|
|
|
|
else
|
|
|
|
nmcli connection up "$name"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-11-17 16:29:34 +00:00
|
|
|
main()
|
2019-07-18 16:45:46 +00:00
|
|
|
{
|
|
|
|
menu=$(build_rofi_menu)
|
2019-11-17 16:29:34 +00:00
|
|
|
choice=$(echo "$menu" | $ROFI)
|
|
|
|
[ "$?" -ne 0 ] && return 1
|
2019-07-18 16:45:46 +00:00
|
|
|
|
2019-11-17 16:29:34 +00:00
|
|
|
name=$(echo "$choice" | cut -d '|' -f 1 | awk '{$1=$1;print}')
|
|
|
|
stat=$(echo "$choice" | cut -d '|' -f 3 | awk '{$1=$1;print}')
|
2019-07-18 16:45:46 +00:00
|
|
|
|
2019-11-17 16:29:34 +00:00
|
|
|
switch_connections "$name" "$stat"
|
2019-07-18 16:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main
|