cli: consolidate active and configured connections (rh #997999)

Handle connection profiles in a single 'show' command instead of 'show active'
and 'show configured'.

nmcli con show [--active] [[id|uuid|path|apath] <bla>]

nmcli con show           : display all connection profiles
nmcli con show --active  : only display active connection profiles
                           (filters out inactive profiles)

nmcli con show myeth     : display details of "myeth" profile, and also active
                           connection info (if the profile is active)
nmcli -f profile con show myeth : only display "myeth"'s static configuration
nmcli -f active  con show myeth : only display active details of "myeth"
nmcli -f connection.id,ipv4,general con show myeth
                                          : display "connection.id"a property
                                            "ipv4" setting and "GENERAL" group
                                            of active data

https://bugzilla.redhat.com/show_bug.cgi?id=997999
This commit is contained in:
Jiří Klimeš
2013-11-07 14:47:12 +01:00
parent 85272df6eb
commit 26f8889286
5 changed files with 636 additions and 416 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -14,7 +14,7 @@
* with this program; if not, write to the Free Software Foundation, Inc., * with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* (C) Copyright 2010 - 2013 Red Hat, Inc. * (C) Copyright 2010 - 2014 Red Hat, Inc.
*/ */
/* Generated configuration file */ /* Generated configuration file */
@@ -72,6 +72,23 @@ nmc_arg_is_help (const char *arg)
return FALSE; return FALSE;
} }
gboolean
nmc_arg_is_option (const char *str, const char *opt_name)
{
const char *p;
if (!str || !*str)
return FALSE;
if (str[0] != '-')
return FALSE;
p = (str[1] == '-') ? str + 2 : str + 1;
return (*p ? (matches (p, opt_name) == 0) : FALSE);
}
/* /*
* Helper function to parse command-line arguments. * Helper function to parse command-line arguments.
* arg_arr: description of arguments to look for * arg_arr: description of arguments to look for
@@ -699,27 +716,15 @@ parse_output_fields (const char *fields_str,
/* Field was not found - error case */ /* Field was not found - error case */
if (fields_array[i].name == NULL) { if (fields_array[i].name == NULL) {
GString *allowed_fields = g_string_sized_new (256);
int k;
/* Set GError */ /* Set GError */
if (idx != -1 && fields_array[idx].group) {
NmcOutputField *second_level = fields_array[idx].group;
for (k = 0; second_level[k].name; k++)
g_string_append_printf (allowed_fields, "%s.%s,",
fields_array[idx].name, second_level[k].name);
} else {
for (k = 0; fields_array[k].name; k++)
g_string_append_printf (allowed_fields, "%s,", fields_array[k].name);
}
g_string_truncate (allowed_fields, allowed_fields->len - 1);
if (!strcasecmp (*iter, "all") || !strcasecmp (*iter, "common")) if (!strcasecmp (*iter, "all") || !strcasecmp (*iter, "common"))
g_set_error (error, NMCLI_ERROR, 0, _("field '%s' has to be alone"), *iter); g_set_error (error, NMCLI_ERROR, 0, _("field '%s' has to be alone"), *iter);
else else {
char *allowed_fields = nmc_get_allowed_fields (fields_array, idx);
g_set_error (error, NMCLI_ERROR, 1, _("invalid field '%s'; allowed fields: %s"), g_set_error (error, NMCLI_ERROR, 1, _("invalid field '%s'; allowed fields: %s"),
*iter, allowed_fields->str); *iter, allowed_fields);
g_string_free (allowed_fields, TRUE); g_free (allowed_fields);
}
/* Free arrays on error */ /* Free arrays on error */
g_array_free (array, TRUE); g_array_free (array, TRUE);
@@ -737,6 +742,35 @@ done:
return array; return array;
} }
/**
* nmc_get_allowed_fields:
* @fields_array: array of fields
* @group_idx: index to the array (for second-level array in 'group' member),
* or -1
*
* Returns: string of allowed fields names.
* Caller is responsible for freeing the array.
*/
char *
nmc_get_allowed_fields (const NmcOutputField fields_array[], int group_idx)
{
GString *allowed_fields = g_string_sized_new (256);
int i;
if (group_idx != -1 && fields_array[group_idx].group) {
NmcOutputField *second_level = fields_array[group_idx].group;
for (i = 0; second_level[i].name; i++)
g_string_append_printf (allowed_fields, "%s.%s,",
fields_array[group_idx].name, second_level[i].name);
} else {
for (i = 0; fields_array[i].name; i++)
g_string_append_printf (allowed_fields, "%s,", fields_array[i].name);
}
g_string_truncate (allowed_fields, allowed_fields->len - 1);
return g_string_free (allowed_fields, FALSE);
}
gboolean gboolean
nmc_terse_option_check (NMCPrintOutput print_output, const char *fields, GError **error) nmc_terse_option_check (NMCPrintOutput print_output, const char *fields, GError **error)
{ {

View File

@@ -14,7 +14,7 @@
* with this program; if not, write to the Free Software Foundation, Inc., * with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* (C) Copyright 2010 - 2013 Red Hat, Inc. * (C) Copyright 2010 - 2014 Red Hat, Inc.
*/ */
#ifndef NMC_UTILS_H #ifndef NMC_UTILS_H
@@ -38,6 +38,7 @@ typedef struct {
int matches (const char *cmd, const char *pattern); int matches (const char *cmd, const char *pattern);
int next_arg (int *argc, char ***argv); int next_arg (int *argc, char ***argv);
gboolean nmc_arg_is_help (const char *arg); gboolean nmc_arg_is_help (const char *arg);
gboolean nmc_arg_is_option (const char *arg, const char *opt_name);
gboolean nmc_parse_args (nmc_arg_t *arg_arr, gboolean last, int *argc, char ***argv, GError **error); gboolean nmc_parse_args (nmc_arg_t *arg_arr, gboolean last, int *argc, char ***argv, GError **error);
char *ssid_to_hex (const char *str, gsize len); char *ssid_to_hex (const char *str, gsize len);
gboolean nmc_string_to_int_base (const char *str, gboolean nmc_string_to_int_base (const char *str,
@@ -85,6 +86,7 @@ GArray *parse_output_fields (const char *fields_str,
gboolean parse_groups, gboolean parse_groups,
GPtrArray **group_fields, GPtrArray **group_fields,
GError **error); GError **error);
char *nmc_get_allowed_fields (const NmcOutputField fields_array[], int group_idx);
gboolean nmc_terse_option_check (NMCPrintOutput print_output, const char *fields, GError **error); gboolean nmc_terse_option_check (NMCPrintOutput print_output, const char *fields, GError **error);
NmcOutputField *nmc_dup_fields_array (NmcOutputField fields[], size_t size, guint32 flags); NmcOutputField *nmc_dup_fields_array (NmcOutputField fields[], size_t size, guint32 flags);
void nmc_empty_output_fields (NmCli *nmc); void nmc_empty_output_fields (NmCli *nmc);

View File

@@ -224,7 +224,7 @@ $ nmcli con add up Team1-slave2
$ nmcli con add con-name my-con-em1 ifname em1 type ethernet ip4 192.168.100.100/24 gw4 192.168.100.1 ip4 1.2.3.4 ip6 abbe::cafe $ nmcli con add con-name my-con-em1 ifname em1 type ethernet ip4 192.168.100.100/24 gw4 192.168.100.1 ip4 1.2.3.4 ip6 abbe::cafe
$ nmcli con mod my-con-em1 ipv4.dns "8.8.8.8 8.8.4.4" $ nmcli con mod my-con-em1 ipv4.dns "8.8.8.8 8.8.4.4"
$ nmcli con mod my-con-em1 ipv6.dns "2001:4860:4860::8888 2001:4860:4860::8844" $ nmcli con mod my-con-em1 ipv6.dns "2001:4860:4860::8888 2001:4860:4860::8844"
$ nmcli -p con show conf my-con-em1 $ nmcli -p con show my-con-em1
</emphasis> </emphasis>
</programlisting> </programlisting>
</example> </example>
@@ -232,8 +232,8 @@ $ nmcli -p con show conf my-con-em1
The first command adds an Ethernet connection profile named <emphasis>my-con-em1</emphasis> The first command adds an Ethernet connection profile named <emphasis>my-con-em1</emphasis>
that is bound to interface name <emphasis>em1</emphasis>. The profile is configured that is bound to interface name <emphasis>em1</emphasis>. The profile is configured
with static IP addresses. The second and third commands modify DNS parameters of the with static IP addresses. The second and third commands modify DNS parameters of the
new connection profile. Using the last <emphasis>con show configured</emphasis> the new connection profile. The last <emphasis>con show</emphasis> command displays the
profile is displayed so that all parameters can be reviewed. profile so that all parameters can be reviewed.
</para> </para>
<example><title>Escaping colon characters in tabular mode</title> <example><title>Escaping colon characters in tabular mode</title>

View File

@@ -19,9 +19,9 @@
.\" with this manual; if not, write to the Free Software Foundation, Inc., .\" with this manual; if not, write to the Free Software Foundation, Inc.,
.\" 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. .\" 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
.\" .\"
.\" Copyright (C) 2010 - 2013 Red Hat, Inc. .\" Copyright (C) 2010 - 2014 Red Hat, Inc.
.\" .\"
.TH NMCLI "1" "12 December 2013" .TH NMCLI "1" "14 January 2014"
.SH NAME .SH NAME
nmcli \- command\(hyline tool for controlling NetworkManager nmcli \- command\(hyline tool for controlling NetworkManager
@@ -97,8 +97,7 @@ producing more structured information, that cannot be displayed on a single
line, default is \fImultiline\fP. Currently, they are: line, default is \fImultiline\fP. Currently, they are:
.br .br
.nf .nf
'nmcli connection show configured <ID>' 'nmcli connection show <ID>'
'nmcli connection show active <ID>'
'nmcli device show' 'nmcli device show'
.fi .fi
\fItabular\fP \(en Output is a table where each line describes a single entry. \fItabular\fP \(en Output is a table where each line describes a single entry.
@@ -276,12 +275,21 @@ connected to the DHCP-enabled network the user would run "nmcli con up default"
.sp .sp
.RS .RS
.TP .TP
.B show active [[ id | uuid | path | apath ] <ID>] .B show [--active]
.br .br
Shows connections which are currently used by a device to connect to a network. List in-memory and on-disk connection profiles, some of which may also be
Without a parameter, all active connections are listed. In order to show the active if a device is using that connection profile. Without a parameter, all
connection details, \fI<ID>\fP must be provided. \fIid\fP, \fIuuid\fP, profiles are listed. When --active option is specified, only the active profiles
\fIpath\fP and \fIapath\fP keywords can be used if \fI<ID>\fP is ambiguous. are shown.
.TP
.B show [--active] [ id | uuid | path | apath ] <ID> ...
.br
Show details for specified connections. By default, both static configuration
and active connection data are displayed. When --active option is specified,
only the active profiles are taken into
account.
\fIid\fP, \fIuuid\fP, \fIpath\fP and \fIapath\fP keywords can be used if
\fI<ID>\fP is ambiguous.
.RS .RS
.PP .PP
Optional <ID>-specifying keywords are: Optional <ID>-specifying keywords are:
@@ -295,19 +303,24 @@ in the format of /org/freedesktop/NetworkManager/Settings/<num> or just <num>
.IP \fIapath\fP 13 .IP \fIapath\fP 13
\(en the <ID> denotes a D-Bus active connection path \(en the <ID> denotes a D-Bus active connection path
in the format of /org/freedesktop/NetworkManager/ActiveConnection/<num> or just <num> in the format of /org/freedesktop/NetworkManager/ActiveConnection/<num> or just <num>
.PP
It is possible to filter the output using the global \fI--fields\fP option. Use the following
values:
.RE .RE
.TP .RS
.B show configured [[ id | uuid | path ] <ID>] .PP
.br .IP \fIprofile\fP 13
Shows in-memory and on-disk connections, some of which may also be \fIactive\fP \(en only shows static profile configuration
if a device is using that connection. Without a parameter, all connections .IP \fIactive\fP 13
are listed. In order to show connection details, \fI<ID>\fP must be \(en only shows active connection data (when the profile is active)
provided. \fIid\fP, \fIuuid\fP and \fIpath\fP keywords can be used if .PP
\fI<ID>\fP is ambiguous. See \fBshow active\fP above for the description of You can also specify particular fields. For static configuration, use setting and property names
the keywords. as described in \fInm-settings\fP(5) manual page. For active data use GENERAL, IP4, DHCP4, IP6,
.br DHCP6, VPN.
.PP
When no command is given to the \fIconnection\fP object, the default action When no command is given to the \fIconnection\fP object, the default action
is 'nmcli connection show configured'. is 'nmcli connection show'.
.RE
.TP .TP
.B up [ id | uuid | path ] <ID> [ifname <ifname>] [ap <BSSID>] [nsp <name>] .B up [ id | uuid | path ] <ID> [ifname <ifname>] [ap <BSSID>] [nsp <name>]
.RE .RE
@@ -327,7 +340,7 @@ connection.
.br .br
If '--wait' option is not specified, the default timeout will be 90 seconds. If '--wait' option is not specified, the default timeout will be 90 seconds.
.br .br
See \fBshow active\fP above for the description of the <ID>-specifying keywords. See \fBconnection show\fP above for the description of the <ID>-specifying keywords.
.RS .RS
.PP .PP
Available options are: Available options are:
@@ -357,7 +370,7 @@ The connection is identified by its name, UUID or D-Bus path.
If <ID> is ambiguous, a keyword \fIid\fP, \fIuuid\fP, \fIpath\fP or If <ID> is ambiguous, a keyword \fIid\fP, \fIuuid\fP, \fIpath\fP or
\fIapath\fP can be used. \fIapath\fP can be used.
.br .br
See \fBshow active\fP above for the description of the <ID>-specifying keywords. See \fBconnection show\fP above for the description of the <ID>-specifying keywords.
.TP .TP
.B add COMMON_OPTIONS TYPE_SPECIFIC_OPTIONS IP_OPTIONS .B add COMMON_OPTIONS TYPE_SPECIFIC_OPTIONS IP_OPTIONS
.br .br
@@ -583,7 +596,7 @@ Edit an existing connection or add a new one, using an interactive editor.
.br .br
The existing connection is identified by its name, UUID or D-Bus path. The existing connection is identified by its name, UUID or D-Bus path.
If <ID> is ambiguous, a keyword \fIid\fP, \fIuuid\fP, or \fIpath\fP can be used. If <ID> is ambiguous, a keyword \fIid\fP, \fIuuid\fP, or \fIpath\fP can be used.
See \fBshow active\fP above for the description of the <ID>-specifying keywords. See \fBconnection show\fP above for the description of the <ID>-specifying keywords.
Not providing an <ID> means that a new connection will be added. Not providing an <ID> means that a new connection will be added.
.sp .sp
The interactive editor will guide you through the connection editing and The interactive editor will guide you through the connection editing and
@@ -621,7 +634,7 @@ Delete a configured connection. The connection to be deleted is identified by
its name, UUID or D-Bus path. If <ID> is ambiguous, a keyword \fIid\fP, its name, UUID or D-Bus path. If <ID> is ambiguous, a keyword \fIid\fP,
\fIuuid\fP or \fIpath\fP can be used. \fIuuid\fP or \fIpath\fP can be used.
.br .br
See \fBshow active\fP above for the description of the <ID>-specifying keywords. See \fBconnection show\fP above for the description of the <ID>-specifying keywords.
.TP .TP
.B reload .B reload
.br .br
@@ -791,34 +804,38 @@ shows the overall status of NetworkManager.
.IP .IP
switches Wi\(hyFi off. switches Wi\(hyFi off.
.IP "\fB\f(CWnmcli connection show configured\fP\fP" .IP "\fB\f(CWnmcli connection show\fP\fP"
.IP .IP
lists all connections NetworkManager has. lists all connections NetworkManager has.
.IP "\fB\f(CWnmcli \-p \-m multiline \-f all con show c\fP\fP" .IP "\fB\f(CWnmcli \-p \-m multiline \-f all con show\fP\fP"
.IP .IP
shows all configured connections in multi-line mode. shows all configured connections in multi-line mode.
.IP "\fB\f(CWnmcli \-p connection show active\fP\fP" .IP "\fB\f(CWnmcli connection show --active\fP\fP"
.IP .IP
lists all currently active connections. lists all currently active connections.
.IP "\fB\f(CWnmcli \-p connection show active \(dq\&My default em1\(dq\&\fP\fP" .IP "\fB\f(CWnmcli \-f name,autoconnect c s\fP\fP"
.IP
shows all connection profile names and their auto-connect property.
.IP "\fB\f(CWnmcli \-p connection show \(dq\&My default em1\(dq\&\fP\fP"
.IP
shows details for "My default em1" connection profile.
.IP "\fB\f(CWnmcli \-f active connection show \(dq\&My default em1\(dq\&\fP\fP"
.IP .IP
shows details for "My default em1" active connection, like IP, DHCP shows details for "My default em1" active connection, like IP, DHCP
information. information, etc.
.IP "\fB\f(CWnmcli \-f name,autoconnect c s c\fP\fP" .IP "\fB\f(CWnmcli -f profile con s \(dq\&My wired connection\(dq\&\fP\fP"
.IP .IP
shows all connections' names and their auto-connect settings. shows static configuration details of the connection profile with "My wired connection" name.
.IP "\fB\f(CWnmcli con s c \(dq\&My wired connection\(dq\&\fP\fP"
.IP
shows all details of the connection with "My wired connection" name.
.IP "\fB\f(CWnmcli \-p con up \(dq\&My wired connection\(dq\& ifname eth0\fP\fP" .IP "\fB\f(CWnmcli \-p con up \(dq\&My wired connection\(dq\& ifname eth0\fP\fP"
.IP .IP
activates the connection with name "My wired connection" on interface eth0. activates the connection profile with name "My wired connection" on interface eth0.
The \-p option makes nmcli show progress of the activation. The \-p option makes nmcli show progress of the activation.
.IP "\fB\f(CWnmcli con up 6b028a27\-6dc9\-4411\-9886\-e9ad1dd43761 ap 00:3A:98:7C:42:D3\fP\fP" .IP "\fB\f(CWnmcli con up 6b028a27\-6dc9\-4411\-9886\-e9ad1dd43761 ap 00:3A:98:7C:42:D3\fP\fP"