Skip to content

Instantly share code, notes, and snippets.

@CIAvash
Created November 27, 2021 18:45
Show Gist options
  • Save CIAvash/868b96eee2a58958b9a586e2c29f4036 to your computer and use it in GitHub Desktop.
Save CIAvash/868b96eee2a58958b9a586e2c29f4036 to your computer and use it in GitHub Desktop.
Switch internet connection using `rofi` and network manager's `nmcli`. Dependencies: `ripgrep`, `hck`, `nmcli`, `rofi`
#!/usr/bin/env bash
connections=$(nmcli connection show | rg -v NAME)
selected_connection_line=$(echo "$connections" | rofi -dmenu -i -p 'Select connection: ')
rofi_exit_code=$?
selected_connection=$(echo "$selected_connection_line" | hck -d ' {2,}' -f 1)
# Disconnect on Alt-1
if [ $rofi_exit_code -eq 10 ]; then
nmcli connection down "$selected_connection"
else
# Disconnect all connections except the selected one
connections_to_disable=$(echo "$connections" | rg -v "$selected_connection")
for connection in $connections_to_disable; do
nmcli connection down "$connection"
done
nmcli connection up "$selected_connection"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment