The 'proper' way to get ONLY my eth0 netmask via CLI in Ubuntu 18.04 on VPS?












1














According to this answer and many other tutorials throughout the web, ifconfig will list lots of fascinating and useful information. And, that seems the standard way to find out stuff like my netmask.



But, for scripting, I need only the netmask itself for eth0; and man ifconfig was unhelpful with this question.



I am using a VPS, so I don't have control of my netmask; I need to find out what it has been set to.



(Note: While I need eth0, some machines might have another, say eth1 or ens3, which would also be useful.)




  • I do not need the entire line the netmask is in:


inet 111.222.333.444 netmask 255.255.255.0 broadcast 111.222.555.666




  • I want to avoid a "hackdoor" method, like getting netmask 255.255.255.0 then running sed "s/netmask //" because stuff can change—unless an expert says so










share|improve this question





























    1














    According to this answer and many other tutorials throughout the web, ifconfig will list lots of fascinating and useful information. And, that seems the standard way to find out stuff like my netmask.



    But, for scripting, I need only the netmask itself for eth0; and man ifconfig was unhelpful with this question.



    I am using a VPS, so I don't have control of my netmask; I need to find out what it has been set to.



    (Note: While I need eth0, some machines might have another, say eth1 or ens3, which would also be useful.)




    • I do not need the entire line the netmask is in:


    inet 111.222.333.444 netmask 255.255.255.0 broadcast 111.222.555.666




    • I want to avoid a "hackdoor" method, like getting netmask 255.255.255.0 then running sed "s/netmask //" because stuff can change—unless an expert says so










    share|improve this question



























      1












      1








      1







      According to this answer and many other tutorials throughout the web, ifconfig will list lots of fascinating and useful information. And, that seems the standard way to find out stuff like my netmask.



      But, for scripting, I need only the netmask itself for eth0; and man ifconfig was unhelpful with this question.



      I am using a VPS, so I don't have control of my netmask; I need to find out what it has been set to.



      (Note: While I need eth0, some machines might have another, say eth1 or ens3, which would also be useful.)




      • I do not need the entire line the netmask is in:


      inet 111.222.333.444 netmask 255.255.255.0 broadcast 111.222.555.666




      • I want to avoid a "hackdoor" method, like getting netmask 255.255.255.0 then running sed "s/netmask //" because stuff can change—unless an expert says so










      share|improve this question















      According to this answer and many other tutorials throughout the web, ifconfig will list lots of fascinating and useful information. And, that seems the standard way to find out stuff like my netmask.



      But, for scripting, I need only the netmask itself for eth0; and man ifconfig was unhelpful with this question.



      I am using a VPS, so I don't have control of my netmask; I need to find out what it has been set to.



      (Note: While I need eth0, some machines might have another, say eth1 or ens3, which would also be useful.)




      • I do not need the entire line the netmask is in:


      inet 111.222.333.444 netmask 255.255.255.0 broadcast 111.222.555.666




      • I want to avoid a "hackdoor" method, like getting netmask 255.255.255.0 then running sed "s/netmask //" because stuff can change—unless an expert says so







      command-line networking server vps ifconfig






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 2 '18 at 14:32

























      asked Dec 2 '18 at 5:34









      Jesse Steele

      12516




      12516






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Probably this is what you need (note this will not work on Ubuntu 16.04):





          #!/bin/bash
          IFACE='eth0'
          ifconfig | grep -A 7 "$IFACE" | sed -nr 's/^.*netmasks([0-9.]+)ssbroadcast.*$/1/p'


          In the first line the name of the network interface is assigned as value of the variable $IFACE - this is useful for scripting, otherwise you can use grep -A 7 'eth0'.



          On the second line the output of the command ifconfig will be piped to the grep command, where the option -A 7 will output the following 7 lines after the line with the matched string/regexp. The output of that command will be piped to sed.



          Within the sed command:




          • the regular expression ^.*netmasks(.*)ssbroadcast.*$ will match to the whole line, from the beginning ^ to the end $, that contains some characters .* and the "keywords" netmaskspace, [0-9.]+ and ssbroadcast in that exact order;


          • that line will be substituted (s/old/new/) with the content of the first capture group [(.*)->1], where the regexp [0-9.]+ will match to the strings that are consisted of digits end dots;


          • the option -r (or -E) will enable the extended regular expressions, which, in this case, will allow us to use the round brackets freely;


          • the option -n with combination of the flag p with output only the matched line and will preserve the rest output of sed.





          Here is an extended example that will parse the names of all network interfaces and will do similar as the above command for each of them:



          for IFACE in $(ifconfig | sed -nr 's/(^[a-z0-9]+):.*/1/p'); do 
          echo -en "${IFACE}:t"; ifconfig |
          grep -A 7 "$IFACE" |
          sed 's/ broadcast.*$//' |
          sed -rn 's/^.*netmask (.*)$/1/p';
          done


          Sample output of the above command executed on a virtual machine with Ubuntu 18.04:



          $ for IFACE in $(ifconfig | sed -nr 's/(^[a-z0-9]+):.*/1/p'); do echo -en "${IFACE}:t"; ifconfig | grep -A 7 "$IFACE" | sed 's/..broadcast.*$//' | sed -rn 's/^.*netmask (.*)$/1/p'; done
          ens33: 255.255.255.0
          lo: 255.0.0.0





          share|improve this answer



















          • 1




            Yep, that's it! You Rock!
            – Jesse Steele
            Dec 2 '18 at 14:31











          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%2f1097834%2fthe-proper-way-to-get-only-my-eth0-netmask-via-cli-in-ubuntu-18-04-on-vps%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Probably this is what you need (note this will not work on Ubuntu 16.04):





          #!/bin/bash
          IFACE='eth0'
          ifconfig | grep -A 7 "$IFACE" | sed -nr 's/^.*netmasks([0-9.]+)ssbroadcast.*$/1/p'


          In the first line the name of the network interface is assigned as value of the variable $IFACE - this is useful for scripting, otherwise you can use grep -A 7 'eth0'.



          On the second line the output of the command ifconfig will be piped to the grep command, where the option -A 7 will output the following 7 lines after the line with the matched string/regexp. The output of that command will be piped to sed.



          Within the sed command:




          • the regular expression ^.*netmasks(.*)ssbroadcast.*$ will match to the whole line, from the beginning ^ to the end $, that contains some characters .* and the "keywords" netmaskspace, [0-9.]+ and ssbroadcast in that exact order;


          • that line will be substituted (s/old/new/) with the content of the first capture group [(.*)->1], where the regexp [0-9.]+ will match to the strings that are consisted of digits end dots;


          • the option -r (or -E) will enable the extended regular expressions, which, in this case, will allow us to use the round brackets freely;


          • the option -n with combination of the flag p with output only the matched line and will preserve the rest output of sed.





          Here is an extended example that will parse the names of all network interfaces and will do similar as the above command for each of them:



          for IFACE in $(ifconfig | sed -nr 's/(^[a-z0-9]+):.*/1/p'); do 
          echo -en "${IFACE}:t"; ifconfig |
          grep -A 7 "$IFACE" |
          sed 's/ broadcast.*$//' |
          sed -rn 's/^.*netmask (.*)$/1/p';
          done


          Sample output of the above command executed on a virtual machine with Ubuntu 18.04:



          $ for IFACE in $(ifconfig | sed -nr 's/(^[a-z0-9]+):.*/1/p'); do echo -en "${IFACE}:t"; ifconfig | grep -A 7 "$IFACE" | sed 's/..broadcast.*$//' | sed -rn 's/^.*netmask (.*)$/1/p'; done
          ens33: 255.255.255.0
          lo: 255.0.0.0





          share|improve this answer



















          • 1




            Yep, that's it! You Rock!
            – Jesse Steele
            Dec 2 '18 at 14:31
















          1














          Probably this is what you need (note this will not work on Ubuntu 16.04):





          #!/bin/bash
          IFACE='eth0'
          ifconfig | grep -A 7 "$IFACE" | sed -nr 's/^.*netmasks([0-9.]+)ssbroadcast.*$/1/p'


          In the first line the name of the network interface is assigned as value of the variable $IFACE - this is useful for scripting, otherwise you can use grep -A 7 'eth0'.



          On the second line the output of the command ifconfig will be piped to the grep command, where the option -A 7 will output the following 7 lines after the line with the matched string/regexp. The output of that command will be piped to sed.



          Within the sed command:




          • the regular expression ^.*netmasks(.*)ssbroadcast.*$ will match to the whole line, from the beginning ^ to the end $, that contains some characters .* and the "keywords" netmaskspace, [0-9.]+ and ssbroadcast in that exact order;


          • that line will be substituted (s/old/new/) with the content of the first capture group [(.*)->1], where the regexp [0-9.]+ will match to the strings that are consisted of digits end dots;


          • the option -r (or -E) will enable the extended regular expressions, which, in this case, will allow us to use the round brackets freely;


          • the option -n with combination of the flag p with output only the matched line and will preserve the rest output of sed.





          Here is an extended example that will parse the names of all network interfaces and will do similar as the above command for each of them:



          for IFACE in $(ifconfig | sed -nr 's/(^[a-z0-9]+):.*/1/p'); do 
          echo -en "${IFACE}:t"; ifconfig |
          grep -A 7 "$IFACE" |
          sed 's/ broadcast.*$//' |
          sed -rn 's/^.*netmask (.*)$/1/p';
          done


          Sample output of the above command executed on a virtual machine with Ubuntu 18.04:



          $ for IFACE in $(ifconfig | sed -nr 's/(^[a-z0-9]+):.*/1/p'); do echo -en "${IFACE}:t"; ifconfig | grep -A 7 "$IFACE" | sed 's/..broadcast.*$//' | sed -rn 's/^.*netmask (.*)$/1/p'; done
          ens33: 255.255.255.0
          lo: 255.0.0.0





          share|improve this answer



















          • 1




            Yep, that's it! You Rock!
            – Jesse Steele
            Dec 2 '18 at 14:31














          1












          1








          1






          Probably this is what you need (note this will not work on Ubuntu 16.04):





          #!/bin/bash
          IFACE='eth0'
          ifconfig | grep -A 7 "$IFACE" | sed -nr 's/^.*netmasks([0-9.]+)ssbroadcast.*$/1/p'


          In the first line the name of the network interface is assigned as value of the variable $IFACE - this is useful for scripting, otherwise you can use grep -A 7 'eth0'.



          On the second line the output of the command ifconfig will be piped to the grep command, where the option -A 7 will output the following 7 lines after the line with the matched string/regexp. The output of that command will be piped to sed.



          Within the sed command:




          • the regular expression ^.*netmasks(.*)ssbroadcast.*$ will match to the whole line, from the beginning ^ to the end $, that contains some characters .* and the "keywords" netmaskspace, [0-9.]+ and ssbroadcast in that exact order;


          • that line will be substituted (s/old/new/) with the content of the first capture group [(.*)->1], where the regexp [0-9.]+ will match to the strings that are consisted of digits end dots;


          • the option -r (or -E) will enable the extended regular expressions, which, in this case, will allow us to use the round brackets freely;


          • the option -n with combination of the flag p with output only the matched line and will preserve the rest output of sed.





          Here is an extended example that will parse the names of all network interfaces and will do similar as the above command for each of them:



          for IFACE in $(ifconfig | sed -nr 's/(^[a-z0-9]+):.*/1/p'); do 
          echo -en "${IFACE}:t"; ifconfig |
          grep -A 7 "$IFACE" |
          sed 's/ broadcast.*$//' |
          sed -rn 's/^.*netmask (.*)$/1/p';
          done


          Sample output of the above command executed on a virtual machine with Ubuntu 18.04:



          $ for IFACE in $(ifconfig | sed -nr 's/(^[a-z0-9]+):.*/1/p'); do echo -en "${IFACE}:t"; ifconfig | grep -A 7 "$IFACE" | sed 's/..broadcast.*$//' | sed -rn 's/^.*netmask (.*)$/1/p'; done
          ens33: 255.255.255.0
          lo: 255.0.0.0





          share|improve this answer














          Probably this is what you need (note this will not work on Ubuntu 16.04):





          #!/bin/bash
          IFACE='eth0'
          ifconfig | grep -A 7 "$IFACE" | sed -nr 's/^.*netmasks([0-9.]+)ssbroadcast.*$/1/p'


          In the first line the name of the network interface is assigned as value of the variable $IFACE - this is useful for scripting, otherwise you can use grep -A 7 'eth0'.



          On the second line the output of the command ifconfig will be piped to the grep command, where the option -A 7 will output the following 7 lines after the line with the matched string/regexp. The output of that command will be piped to sed.



          Within the sed command:




          • the regular expression ^.*netmasks(.*)ssbroadcast.*$ will match to the whole line, from the beginning ^ to the end $, that contains some characters .* and the "keywords" netmaskspace, [0-9.]+ and ssbroadcast in that exact order;


          • that line will be substituted (s/old/new/) with the content of the first capture group [(.*)->1], where the regexp [0-9.]+ will match to the strings that are consisted of digits end dots;


          • the option -r (or -E) will enable the extended regular expressions, which, in this case, will allow us to use the round brackets freely;


          • the option -n with combination of the flag p with output only the matched line and will preserve the rest output of sed.





          Here is an extended example that will parse the names of all network interfaces and will do similar as the above command for each of them:



          for IFACE in $(ifconfig | sed -nr 's/(^[a-z0-9]+):.*/1/p'); do 
          echo -en "${IFACE}:t"; ifconfig |
          grep -A 7 "$IFACE" |
          sed 's/ broadcast.*$//' |
          sed -rn 's/^.*netmask (.*)$/1/p';
          done


          Sample output of the above command executed on a virtual machine with Ubuntu 18.04:



          $ for IFACE in $(ifconfig | sed -nr 's/(^[a-z0-9]+):.*/1/p'); do echo -en "${IFACE}:t"; ifconfig | grep -A 7 "$IFACE" | sed 's/..broadcast.*$//' | sed -rn 's/^.*netmask (.*)$/1/p'; done
          ens33: 255.255.255.0
          lo: 255.0.0.0






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 2 '18 at 14:56

























          answered Dec 2 '18 at 13:14









          pa4080

          13.4k52562




          13.4k52562








          • 1




            Yep, that's it! You Rock!
            – Jesse Steele
            Dec 2 '18 at 14:31














          • 1




            Yep, that's it! You Rock!
            – Jesse Steele
            Dec 2 '18 at 14:31








          1




          1




          Yep, that's it! You Rock!
          – Jesse Steele
          Dec 2 '18 at 14:31




          Yep, that's it! You Rock!
          – Jesse Steele
          Dec 2 '18 at 14:31


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f1097834%2fthe-proper-way-to-get-only-my-eth0-netmask-via-cli-in-ubuntu-18-04-on-vps%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

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents