Lxd: lxc list: split "ip4 (interface)" into separate fields

Created on 14 Jul 2018  路  12Comments  路  Source: lxc/lxd

  • LXC version: 3.2
  • LXD version: 3.2

Issue description

I'd like to get IPv4 address in command line for feeding it to other commands But it doesn't work, because result contains interface name:

$ lxc list -c4 --format csv          
10.35.65.71 (eth0)

So maybe remove (eth0) from output unless --verbose is specified?

Most helpful comment

@techtonik this should work:

a=( lxc list -c4 --format csv ) echo "${a[0]}"

I'm pretty sure echo is available on your system.

All 12 comments

I also think about upgrading lxc info <container> with templates, exposing all information as a tree, to use something like`lxc info --template "$Ipv4[0]".

The recommended way to do this is by using the json output and jq where you can do any filtering you want, including complex filtering maps. We don't intend to duplicate the filtering features of something like jq in the LXD client itself.

One such example with a template that's not too dissimilar from your suggestion would be:

stgraber@castiana:~$ lxc list --format=json snapcraft | jq -r ".[].state.network.eth0.addresses | .[0].address"
10.166.11.120

This particular query gets you the first address on the eth0 interface. jq effectively lets you see, filter and query the property tree of any JSON document and offers an interface that many are already familiar with so I don't think we need to reinvent the wheel here.

jq is not installed by default even in Ubuntu.

Sure but it's very widely available and super light, so not a big deal to install it.

Why not provide identical UX to Kubernetes? https://kubernetes.io/docs/reference/kubectl/jsonpath/

I need this again to automate https://blog.simos.info/how-to-use-the-x2go-remote-desktop-with-lxd-containers/

LXD is not usable out of the box for this case. Requires external utilities - either jq or cut to be present.

@techtonik @stgraber could you use:

lxc list -c4 --format csv | awk '{print $1}'

@tomponline that's not out of the box. awk is not available on all systems.

@techtonik this should work:

a=( lxc list -c4 --format csv ) echo "${a[0]}"

I'm pretty sure echo is available on your system.

The above recipe didn't work, but this one worked:

a=( $(lxc list -c4 --format csv u1) ) ip4=$a[1] echo $ip4

Only for bash I guess.

Was this page helpful?
0 / 5 - 0 ratings