2

I want to have a bash script that can delete all my network manager connections of type gsm with nmcli.

What is the best approach for this?

  • I figured out I can first show all the connections: nmcli c show I can delete a connection by name with the following: nmcli connection delete id So maybe filter the connection names from first command then delete these filtered connection names with second command? I also do not know how to write that... (kinda new with bash) Thanks! – Robbie Valkenburg Apr 16 '21 at 13:06

3 Answers3

1

This is actually a trickier question than it seems on the surface, because NetworkManager allows for connection names with spaces in them. This makes programmatic parsing of the output of nmcli connection show for connection names a bit awkward. I think the best option for scripting would be to rely on the UUID, since it seems to consistently be a 36 character group of hexidecimal characters and dashes. This means we can pull it consistently with a regular expression. So for example you could get a list of the UUIDs for gsm connections with the following:

$ nmcli connection show | grep gsm | grep -E -o '[0-9a-f\-]{36}'
cc823da6-d4e1-4757-a37a-aaaaaaaaa
etc

So you could grab the UUIDs and then delete based on the UUID:

GSM_UUIDS=$(nmcli connection show | grep gsm | grep -E -o '[0-9a-f\-]{36}')
while IFS= read -r UUID; do echo nmcli connection delete $UUID; done <<< "$GSM_UUIDS"

Run with the echo to make sure you're getting the result you expect, then you can remove it and you should be in business. I ran locally with some dummy GSM connections and it seemed to work they way you would want it to:

GSM_UUIDS=$(nmcli connection show | grep gsm | grep -E -o '[0-9a-f\-]{36}')
while IFS= read -r UUID; do nmcli connection delete $UUID; done <<< "$GSM_UUIDS" 
Connection 'gsm' (cd311376-d7ab-4891-ba73-e4e8a3fc6614) successfully deleted.
Connection 'gsm-1' (54171181-5c37-4224-baf5-9eb36458f773) successfully deleted.
  • There is a potential bug here. This code will also match a connection with the name "This is not the gsm connection you are looking for". I also suspect that there is a similar potential issue in the extraction of the UUID. In most cases, these issues may not matter, of course, if all you need is a quick-and-dirty throwaway script. – Kevin Keane Feb 24 '23 at 04:56
0
nmcli con del $(nmcli -t -f UUID,TYPE con | awk -F":" '{if ($2 == "gsm") print $1}')
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Welcome to Stack Overflow, and thank you for contributing an answer. Would you kindly edit your answer to include an explanation of your code? That will help future readers better understand what is going on, and especially those members of the community who are new to the language and struggling to understand the concepts. – Jeremy Caney Nov 25 '21 at 01:47
  • This answer is too cryptic, but otherwise better than the first answer because it is guaranteed not to accidentally pick up a name that matches a UUID format. -f UUID,TYPE only outputs the two fields we are interested in. -t changes the output to omit the header, and uses a colon instead of a space as separator. The awk then splits the UUID from the type and evalutaes the type. The end result is that $(...) produces all the UUIDs for the gsm connections, and that is then fed into the delete command for nmcli. – Kevin Keane Feb 24 '23 at 04:41
0

Elaborating on and de-cryptifying the answer from @Yaroslav752 :

ethernet_uuids=$(nmcli -t -f UUID,TYPE connection show | awk -F":" '{if ($2 == "gsm") print $1}')
nmcli connection delete $ethernet_uuids

The command

nmcli -t -f UUID,TYPE connection show

is a standard connection show, but with format specifiers.

-f UUID,TYPE means: only display the fields UUID and TYPE, and in that order. This avoids edge cases, such as an Ethernet connection with the name "This is not a gsm connection"

-t changes the output format to omit the header, and use a colon instead of a space as separator between the UUID and the TYPE.

The awk expression skips anything where the second field is not the word "gsm". If the second field is gsm, it prints the first field (i.e., the UUID).

My version stores these uuids in a variable, and then passes that variable to the nmcli connection delete command. That's simply for readability; I'm not a big fan of one-liners.

Kevin Keane
  • 1,506
  • 12
  • 24