Get uuid of connection after adding it using nmcli

Septatrix asked:

I would like to add VPN connections to multiple machines and let them activate automatically when when using the default ethernet connection. However I need the UUID of the VPN connection I just added to configure the automatic connection.

What I have so far is importing the VPN connection using:

$ sudo nmcli connection import type openvpn file some_dynamic_name.ovpn
Verbindung »some_dynamic_name« (0724d07e-4a31-488d-91e8-fd6031679dd6) erfolgreich hinzugefügt.

Where some_dynamic_name should be the connection.id and the uuid corresponds to the connection.uuid. Given this UUID I can now configure my target connection to automatically use the VPN using:

$ nmcli connection modify "Kabelgebundene Verbindung 1" connection.secondaries 0724d07e-4a31-488d-91e8-fd6031679dd6

Theoretically I could parse the output and pass it to the second command though I do not know if the output can be considered stable and I would like to add this to a script for unattended setup. Can I somehow specify the output format for the response? I know I can pass --get-values connection.uuid to the import command (directly after nmcli) though that does not change anything

My answer:


Since you know the name of the connection, you can use -g / --get-values to get its UUID after it has been created.

# nmcli --get-values connection.uuid c show "some_dynamic_name"
0724d07e-4a31-488d-91e8-fd6031679dd6

You could do it in one shot with shell substitution, something like this:

# nmcli c mod "Kabelgebundene Verbindung 1" connection.secondaries $(nmcli -g connection.uuid c show "some_dynamic_name")

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.