How do I get the IP address of an LXC container for automation?
How can I get the IP address of an LXC container in a format I can use in scripting?
Right now, the command lxc info <container>
report that information, but in a human readable format, with a lot of information.
I would like to ONLY to GET the IP address given a container name.
Note: I HAVE to duplicate this question because Linux Containers have changed a lot.
Installing lxd and using unprivileged containers is the default way to go this days (2017) and I think the solutions posted on the original question are do not resolve the issue in this case.
In any case, I installed the package lxc1 to get access to the command lxc-info
, but that command doesn't recognize any of my unprivileged containers.
A native solution (which isn't any prettier than @siloko's answer) would be
lxc list "<name>" -c 4 | awk '!/IPV4/{ if ( $2 != "" ) print $2}'
There are alternatives to awk
, but that's tangential to the question.
So far this is the easiest way:
lxc list -c4 --format csv <container> | cut -d' ' -f1
But maybe it will be possible without cut
.
EDIT: Uncut bash:
a=( $(lxc list -c4 --format csv u1) ) ip4=$a[1] echo $ip4
Hint from @monstermunchkin from the above issue.