Comment 1 for bug 1338382

Revision history for this message
Lonnie Lee Best (launchpad-startport) wrote :

If you are connected to a VPN, this command will return a list of established VPN connections:
nmcli con status | grep -oP '^(([^\s]+\s){1,}[\s]{2,}){4}yes.+$'

This command will list the IDs of any established vpn connections (one per line):
nmcli con status | grep -oP '^(([^\s]+\s){1,}[\s]{2,}){4}yes.+$' | grep -oP '^.*?(?=\s{2,})'

So to disconnect all VPN connections you could do it based on this ID like this:
nmcli con status | grep -oP '^(([^\s]+\s){1,}[\s]{2,}){4}yes.+$' | grep -oP '^.*?(?=\s{2,})' | sed 's/^/\x27/' | sed 's/$/\x27/' | xargs -L1 nmcli con down id

However, perhaps it is better to do this using the UUIDs.

You can list the UUIDs of all established VPN connections like this:
nmcli con status | grep -oP '^(([^\s]+\s){1,}[\s]{2,}){4}yes.+$' | grep -oP '(?<=[\s]{2})[^\s]+(?=(\s+[^\s]+){2}\s+yes.*)'

So you can disconnect all established VPN connections by UUID like this:
nmcli con status | grep -oP '^(([^\s]+\s){1,}[\s]{2,}){4}yes.+$' | grep -oP '(?<=[\s]{2})[^\s]+(?=(\s+[^\s]+){2}\s+yes.*)' | xargs -L1 nmcli con down uuid

Perhaps this logic could be added to nmcli, so that this following proposed option would concisely achieve the same results:
nmcli con down allvpn