15

I'm wondering how to display a connection status using nmcli. I understand that the following will display a list of configured connections:

nmcli con show

And I also understand that the following will show only active connections:

nmcli con show --active

And that the following will display all settings for a connection (which is a very long list):

nmcli con show {connection_name}

My question is: Is there a quick way to display the status of a connection? Something similar to:

nmcli con status {connection_name}

Noting that the above is actually not a valid option on CentOS or Fedora.

Matthias Braun
  • 7,797
  • 7
  • 45
  • 54
Khaled Abuelenain
  • 173
  • 1
  • 2
  • 8
  • 1
    Did you look at the `--fields` option? Something like `nmcli -f field_name con show {connection_name}`. If you give an invalid value for `field_name` some versions will print all valid field names. Depending on what you get you can possibly further refine by piping through `grep` or `sed` or the like. – B Layer May 05 '18 at 18:37
  • @B Layer Tried that but the --fields option only prints specific columns in the output, so if the argument --f ACTIVE is given, for example, and there are two active connections, it will print yes yes on two separate lines. That is it. – Khaled Abuelenain Jun 09 '18 at 03:34
  • 1
    What do you want it to print in that case? It might be a good idea to put an example of the output you want in your question. BTW, did you try exactly what I metioned? `nmcli -f {field_name} con show {connection_name}`? And did try the other suggestion where you give an invalid field name? – B Layer Jun 09 '18 at 04:27

2 Answers2

15

As user B Layer suggested in their comment, you can specify a field name with nmcli.

I think the most relevant field in your case is GENERAL.STATE:

nmcli -f GENERAL.STATE con show {connection_name}

For my current connection, this yields:

GENERAL.STATE: activated

Matthias Braun
  • 7,797
  • 7
  • 45
  • 54
2

If you need a true/false state, you could use nmcli's -g option in combination with grep's -q as follows:

nmcli -g GENERAL.STATE c s interface|grep -q 'activ'

Note the lack of e at the end of the word active. It is meant for future proofing if the word activated changes to for example active or activity or some other form at some point.

GGets
  • 159
  • 1
  • 8