How do I get the IP address of an LXC container for automation?












3















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.










share|improve this question



























    3















    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.










    share|improve this question

























      3












      3








      3


      1






      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.










      share|improve this question














      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.







      lxc lxd






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 1 '17 at 14:36









      jgomo3jgomo3

      4261622




      4261622






















          5 Answers
          5






          active

          oldest

          votes


















          2














          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.






          share|improve this answer































            2














            lxc list | grep nameofthecontainer | egrep -o "[0-9]+.[0-9]+.[0-9]+.[0-9]+"


            This is what I am using, I pass the container name in as a variable.






            share|improve this answer





















            • 2





              the grep command is unneded, you can put directly lxc list container_name | egrep -o "[0-9]+.[0-9]+.[0-9]+.[0-9]+" with the same result

              – Yonsy Solis
              Feb 20 '18 at 15:45





















            1














            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.






            share|improve this answer

































              0














              Probably a bit ugly but:



              lxc-info -n my-container | grep IP: | tr -d ' ' | cut -f2 -d:


              will get you just the IP address






              share|improve this answer
























              • Thank you. It would be a solution in the near time. BTW, the idea is to use the command lxc info, not lxc-info as they are different how I explained.

                – jgomo3
                Jun 1 '17 at 14:58











              • bare lxc is not available on my system (Ubuntu 16.04), sorry.

                – siloko
                Jun 2 '17 at 6:55



















              -1














              lxc-info --name container --ips --no-humanize


              prints the container IP addresses.



              The returned value is a list because a container can have more than one address.






              share|improve this answer























                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "89"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f921110%2fhow-do-i-get-the-ip-address-of-an-lxc-container-for-automation%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                2














                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.






                share|improve this answer




























                  2














                  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.






                  share|improve this answer


























                    2












                    2








                    2







                    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.






                    share|improve this answer













                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jun 13 '17 at 21:27









                    Jonathan Y.Jonathan Y.

                    504928




                    504928

























                        2














                        lxc list | grep nameofthecontainer | egrep -o "[0-9]+.[0-9]+.[0-9]+.[0-9]+"


                        This is what I am using, I pass the container name in as a variable.






                        share|improve this answer





















                        • 2





                          the grep command is unneded, you can put directly lxc list container_name | egrep -o "[0-9]+.[0-9]+.[0-9]+.[0-9]+" with the same result

                          – Yonsy Solis
                          Feb 20 '18 at 15:45


















                        2














                        lxc list | grep nameofthecontainer | egrep -o "[0-9]+.[0-9]+.[0-9]+.[0-9]+"


                        This is what I am using, I pass the container name in as a variable.






                        share|improve this answer





















                        • 2





                          the grep command is unneded, you can put directly lxc list container_name | egrep -o "[0-9]+.[0-9]+.[0-9]+.[0-9]+" with the same result

                          – Yonsy Solis
                          Feb 20 '18 at 15:45
















                        2












                        2








                        2







                        lxc list | grep nameofthecontainer | egrep -o "[0-9]+.[0-9]+.[0-9]+.[0-9]+"


                        This is what I am using, I pass the container name in as a variable.






                        share|improve this answer















                        lxc list | grep nameofthecontainer | egrep -o "[0-9]+.[0-9]+.[0-9]+.[0-9]+"


                        This is what I am using, I pass the container name in as a variable.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Feb 2 '18 at 2:13









                        muru

                        1




                        1










                        answered Feb 1 '18 at 23:55









                        NeilNeil

                        211




                        211








                        • 2





                          the grep command is unneded, you can put directly lxc list container_name | egrep -o "[0-9]+.[0-9]+.[0-9]+.[0-9]+" with the same result

                          – Yonsy Solis
                          Feb 20 '18 at 15:45
















                        • 2





                          the grep command is unneded, you can put directly lxc list container_name | egrep -o "[0-9]+.[0-9]+.[0-9]+.[0-9]+" with the same result

                          – Yonsy Solis
                          Feb 20 '18 at 15:45










                        2




                        2





                        the grep command is unneded, you can put directly lxc list container_name | egrep -o "[0-9]+.[0-9]+.[0-9]+.[0-9]+" with the same result

                        – Yonsy Solis
                        Feb 20 '18 at 15:45







                        the grep command is unneded, you can put directly lxc list container_name | egrep -o "[0-9]+.[0-9]+.[0-9]+.[0-9]+" with the same result

                        – Yonsy Solis
                        Feb 20 '18 at 15:45













                        1














                        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.






                        share|improve this answer






























                          1














                          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.






                          share|improve this answer




























                            1












                            1








                            1







                            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.






                            share|improve this answer















                            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.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 29 at 10:39

























                            answered Jul 14 '18 at 1:23









                            anatoly techtonikanatoly techtonik

                            84121431




                            84121431























                                0














                                Probably a bit ugly but:



                                lxc-info -n my-container | grep IP: | tr -d ' ' | cut -f2 -d:


                                will get you just the IP address






                                share|improve this answer
























                                • Thank you. It would be a solution in the near time. BTW, the idea is to use the command lxc info, not lxc-info as they are different how I explained.

                                  – jgomo3
                                  Jun 1 '17 at 14:58











                                • bare lxc is not available on my system (Ubuntu 16.04), sorry.

                                  – siloko
                                  Jun 2 '17 at 6:55
















                                0














                                Probably a bit ugly but:



                                lxc-info -n my-container | grep IP: | tr -d ' ' | cut -f2 -d:


                                will get you just the IP address






                                share|improve this answer
























                                • Thank you. It would be a solution in the near time. BTW, the idea is to use the command lxc info, not lxc-info as they are different how I explained.

                                  – jgomo3
                                  Jun 1 '17 at 14:58











                                • bare lxc is not available on my system (Ubuntu 16.04), sorry.

                                  – siloko
                                  Jun 2 '17 at 6:55














                                0












                                0








                                0







                                Probably a bit ugly but:



                                lxc-info -n my-container | grep IP: | tr -d ' ' | cut -f2 -d:


                                will get you just the IP address






                                share|improve this answer













                                Probably a bit ugly but:



                                lxc-info -n my-container | grep IP: | tr -d ' ' | cut -f2 -d:


                                will get you just the IP address







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jun 1 '17 at 14:54









                                silokosiloko

                                50727




                                50727













                                • Thank you. It would be a solution in the near time. BTW, the idea is to use the command lxc info, not lxc-info as they are different how I explained.

                                  – jgomo3
                                  Jun 1 '17 at 14:58











                                • bare lxc is not available on my system (Ubuntu 16.04), sorry.

                                  – siloko
                                  Jun 2 '17 at 6:55



















                                • Thank you. It would be a solution in the near time. BTW, the idea is to use the command lxc info, not lxc-info as they are different how I explained.

                                  – jgomo3
                                  Jun 1 '17 at 14:58











                                • bare lxc is not available on my system (Ubuntu 16.04), sorry.

                                  – siloko
                                  Jun 2 '17 at 6:55

















                                Thank you. It would be a solution in the near time. BTW, the idea is to use the command lxc info, not lxc-info as they are different how I explained.

                                – jgomo3
                                Jun 1 '17 at 14:58





                                Thank you. It would be a solution in the near time. BTW, the idea is to use the command lxc info, not lxc-info as they are different how I explained.

                                – jgomo3
                                Jun 1 '17 at 14:58













                                bare lxc is not available on my system (Ubuntu 16.04), sorry.

                                – siloko
                                Jun 2 '17 at 6:55





                                bare lxc is not available on my system (Ubuntu 16.04), sorry.

                                – siloko
                                Jun 2 '17 at 6:55











                                -1














                                lxc-info --name container --ips --no-humanize


                                prints the container IP addresses.



                                The returned value is a list because a container can have more than one address.






                                share|improve this answer




























                                  -1














                                  lxc-info --name container --ips --no-humanize


                                  prints the container IP addresses.



                                  The returned value is a list because a container can have more than one address.






                                  share|improve this answer


























                                    -1












                                    -1








                                    -1







                                    lxc-info --name container --ips --no-humanize


                                    prints the container IP addresses.



                                    The returned value is a list because a container can have more than one address.






                                    share|improve this answer













                                    lxc-info --name container --ips --no-humanize


                                    prints the container IP addresses.



                                    The returned value is a list because a container can have more than one address.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jan 17 '18 at 17:56









                                    G. FiedlerG. Fiedler

                                    992




                                    992






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Ask Ubuntu!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f921110%2fhow-do-i-get-the-ip-address-of-an-lxc-container-for-automation%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                                        Mangá

                                        Eduardo VII do Reino Unido