How to display the IP address of the default Interface with Internet connection?












5















I need to create a script that outputs the internal IP address, that is configured as the default Interface.










share|improve this question

























  • Do you mean the one used for sending to the default gateway?

    – Hauke Laging
    Nov 9 '14 at 18:02











  • possible duplicate of How can I get my external IP address in bash?

    – jasonwryan
    Nov 9 '14 at 18:03






  • 1





    @jasonwryan not a dupe (or not a dupe of that one anyway), the OP wants the internal IP, not the external.

    – terdon
    Nov 9 '14 at 18:11













  • Hauke, this might be it, if the interpretation of the "default" value from the route command is that that is.

    – Marcello de Sales
    Nov 9 '14 at 18:11






  • 1





    @MarcellodeSales no, it has no dependencies. It is just looking for something different. You are showing how to get the IP of a machine in the internal network while the dupe is about getting the external IP. Two very different things. Jason was confused because your original question was asking for a "public" IP which is not what your answer returns.

    – terdon
    Nov 9 '14 at 18:17
















5















I need to create a script that outputs the internal IP address, that is configured as the default Interface.










share|improve this question

























  • Do you mean the one used for sending to the default gateway?

    – Hauke Laging
    Nov 9 '14 at 18:02











  • possible duplicate of How can I get my external IP address in bash?

    – jasonwryan
    Nov 9 '14 at 18:03






  • 1





    @jasonwryan not a dupe (or not a dupe of that one anyway), the OP wants the internal IP, not the external.

    – terdon
    Nov 9 '14 at 18:11













  • Hauke, this might be it, if the interpretation of the "default" value from the route command is that that is.

    – Marcello de Sales
    Nov 9 '14 at 18:11






  • 1





    @MarcellodeSales no, it has no dependencies. It is just looking for something different. You are showing how to get the IP of a machine in the internal network while the dupe is about getting the external IP. Two very different things. Jason was confused because your original question was asking for a "public" IP which is not what your answer returns.

    – terdon
    Nov 9 '14 at 18:17














5












5








5


1






I need to create a script that outputs the internal IP address, that is configured as the default Interface.










share|improve this question
















I need to create a script that outputs the internal IP address, that is configured as the default Interface.







networking scripting ip ifconfig






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 '14 at 18:12









terdon

130k32254432




130k32254432










asked Nov 9 '14 at 17:59









Marcello de SalesMarcello de Sales

14816




14816













  • Do you mean the one used for sending to the default gateway?

    – Hauke Laging
    Nov 9 '14 at 18:02











  • possible duplicate of How can I get my external IP address in bash?

    – jasonwryan
    Nov 9 '14 at 18:03






  • 1





    @jasonwryan not a dupe (or not a dupe of that one anyway), the OP wants the internal IP, not the external.

    – terdon
    Nov 9 '14 at 18:11













  • Hauke, this might be it, if the interpretation of the "default" value from the route command is that that is.

    – Marcello de Sales
    Nov 9 '14 at 18:11






  • 1





    @MarcellodeSales no, it has no dependencies. It is just looking for something different. You are showing how to get the IP of a machine in the internal network while the dupe is about getting the external IP. Two very different things. Jason was confused because your original question was asking for a "public" IP which is not what your answer returns.

    – terdon
    Nov 9 '14 at 18:17



















  • Do you mean the one used for sending to the default gateway?

    – Hauke Laging
    Nov 9 '14 at 18:02











  • possible duplicate of How can I get my external IP address in bash?

    – jasonwryan
    Nov 9 '14 at 18:03






  • 1





    @jasonwryan not a dupe (or not a dupe of that one anyway), the OP wants the internal IP, not the external.

    – terdon
    Nov 9 '14 at 18:11













  • Hauke, this might be it, if the interpretation of the "default" value from the route command is that that is.

    – Marcello de Sales
    Nov 9 '14 at 18:11






  • 1





    @MarcellodeSales no, it has no dependencies. It is just looking for something different. You are showing how to get the IP of a machine in the internal network while the dupe is about getting the external IP. Two very different things. Jason was confused because your original question was asking for a "public" IP which is not what your answer returns.

    – terdon
    Nov 9 '14 at 18:17

















Do you mean the one used for sending to the default gateway?

– Hauke Laging
Nov 9 '14 at 18:02





Do you mean the one used for sending to the default gateway?

– Hauke Laging
Nov 9 '14 at 18:02













possible duplicate of How can I get my external IP address in bash?

– jasonwryan
Nov 9 '14 at 18:03





possible duplicate of How can I get my external IP address in bash?

– jasonwryan
Nov 9 '14 at 18:03




1




1





@jasonwryan not a dupe (or not a dupe of that one anyway), the OP wants the internal IP, not the external.

– terdon
Nov 9 '14 at 18:11







@jasonwryan not a dupe (or not a dupe of that one anyway), the OP wants the internal IP, not the external.

– terdon
Nov 9 '14 at 18:11















Hauke, this might be it, if the interpretation of the "default" value from the route command is that that is.

– Marcello de Sales
Nov 9 '14 at 18:11





Hauke, this might be it, if the interpretation of the "default" value from the route command is that that is.

– Marcello de Sales
Nov 9 '14 at 18:11




1




1





@MarcellodeSales no, it has no dependencies. It is just looking for something different. You are showing how to get the IP of a machine in the internal network while the dupe is about getting the external IP. Two very different things. Jason was confused because your original question was asking for a "public" IP which is not what your answer returns.

– terdon
Nov 9 '14 at 18:17





@MarcellodeSales no, it has no dependencies. It is just looking for something different. You are showing how to get the IP of a machine in the internal network while the dupe is about getting the external IP. Two very different things. Jason was confused because your original question was asking for a "public" IP which is not what your answer returns.

– terdon
Nov 9 '14 at 18:17










7 Answers
7






active

oldest

votes


















5














Here's another slightly terser method using procfs (assumes you're using Linux):



default_iface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
ip addr show dev "$default_iface" | awk '$1 ~ /^inet/ { sub("/.*", "", $2); print $2 }'


This returns both the IPv4 and (if available) the IPv6 address of the interface. You can change the test if you only want one or the other (look for inet for IPv4, and inet6 for IPv6).





$ default_iface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
$ ip addr show dev "$default_iface" | awk '$1 ~ /^inet/ { sub("/.*", "", $2); print $2 }'
10.0.2.15
fe80::a00:27ff:fe45:b085




$ ip addr show dev "$default_iface" | awk '$1 == "inet" { sub("/.*", "", $2); print $2 }'
10.0.2.15




$ ip addr show dev "$default_iface" | awk '$1 == "inet6" { sub("/.*", "", $2); print $2 }'
fe80::a00:27ff:fe45:b085





share|improve this answer

































    1














    Here's what I wrote:




    1. Get the default interface from the "route" command.


    It will print out which interface is the "default". For my host, I need to get the last column of the default line.



    [root@pppdc9prd3ga mdesales]# route
    Kernel IP routing table
    Destination Gateway Genmask Flags Metric Ref Use Iface
    192.168.4.0 * 255.255.252.0 U 0 0 0 bridge0
    10.132.60.0 * 255.255.252.0 U 0 0 0 eth4
    link-local * 255.255.0.0 U 1002 0 0 eth4
    link-local * 255.255.0.0 U 1003 0 0 bridge0
    default 10.132.60.1 0.0.0.0 UG 0 0 0 eth4



    1. Use "ifconfig" to retrieve the IP address of that interface.


    Just getting the addr: value.



    [root@pppdc9prd3ga mdesales]# ifconfig eth4
    eth4 Link encap:Ethernet HWaddr 00:50:56:01:42:91
    inet addr:10.132.63.191 Bcast:10.132.63.255 Mask:255.255.252.0
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:1346288 errors:0 dropped:0 overruns:0 frame:0
    TX packets:438844 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:276243478 (263.4 MiB) TX bytes:116188062 (110.8 MiB)


    So here's the script I came up with.



    /app/myPublicIp.sh 
    defaultInterface=$(route | grep default | awk '{print $(NF)}')
    ifconfig $defaultInterface | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'


    Here's it executing:



    /app/ipFor.sh 
    10.132.63.191


    Hope it helps!






    share|improve this answer
























    • Nice, +1. You could simplify (well, shorten it anyway) that to ifconfig $(route | grep -oP '^default.*s+K.*') | grep -oP 'inet addr:K[^s]+')

      – terdon
      Nov 9 '14 at 18:15











    • If you're going to do this on Linux, you might consider using procfs instead of parsing :-)

      – Chris Down
      Nov 9 '14 at 18:26













    • @ChrisDown... would that be via /proc/net? Yeah, this is for a Docker cluster.

      – Marcello de Sales
      Nov 9 '14 at 18:39













    • @MarcellodeSales Yeah, /proc/net/route. I've posted an answer to that effect.

      – Chris Down
      Nov 9 '14 at 18:51



















    1














    Lots of good answers here, but wanted to throw in my usual approach:



    The simplest solution is to get the route for a public internet address:



    $ ip route get 1.1.1.1 | grep -oP 'src KS+'
    192.168.0.20


    Another solution is to get the default gateway, and then get the IP addr used to communicate with that gateway:



    $ ip route get $(ip route show 0.0.0.0/0 | grep -oP 'via KS+') | grep -oP 'src KS+'
    192.168.0.20





    share|improve this answer































      0














      If what you want is the IP address assigned to the default interface (which is what I understood from the comments under the question), using the Swiss army knife of network setup (ip) should be enough:



      $ ip route | grep '^default'
      default via 10.176.143.1 dev eth1 metric 203
      $ ip addr show eth1
      4: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
      link/ether c0:de:f1:72:30:48 brd ff:ff:ff:ff:ff:ff
      inet 10.176.143.71/24 brd 10.176.143.255 scope global eth1
      valid_lft forever preferred_lft forever


      This shortens to



      $ ip addr show 
      $( /sbin/ip route
      | grep '^default'
      | sed 's/^.*dev ([^ t]*) .*$/1/' )


      which is ugly, because it is parsing something that probably wasn't really meant to be parsed (output of ip route), but should work.






      share|improve this answer































        0














        My favorite one is following.



        Get the default interface:



        $ ip r | grep -oP 'default .* K.+'
        eth0


        Get the ip of an interface:



        $ ip a show eth0 | grep -oP 'inet K[d.]+'
        10.33.44.135


        Combined:



        $ ip a show $(ip r | grep -oP 'default .* K.+') | grep -oP 'inet K[d.]+'
        10.33.44.135





        share|improve this answer































          0














          Simple Command with default interface.



          ip route | grep src | awk '{print $NF; exit}'


          Tested on All Unix OS






          share|improve this answer

































            0














            This worked for me on Centos 7. Find default interface using ip



            ifconfig $(ip route | awk '/default/ { print $5 }') | grep "inet " | awk '{print $2}'





            share|improve this answer








            New contributor




            N A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.




















              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "106"
              };
              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: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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%2funix.stackexchange.com%2fquestions%2f166999%2fhow-to-display-the-ip-address-of-the-default-interface-with-internet-connection%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              7 Answers
              7






              active

              oldest

              votes








              7 Answers
              7






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              Here's another slightly terser method using procfs (assumes you're using Linux):



              default_iface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
              ip addr show dev "$default_iface" | awk '$1 ~ /^inet/ { sub("/.*", "", $2); print $2 }'


              This returns both the IPv4 and (if available) the IPv6 address of the interface. You can change the test if you only want one or the other (look for inet for IPv4, and inet6 for IPv6).





              $ default_iface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
              $ ip addr show dev "$default_iface" | awk '$1 ~ /^inet/ { sub("/.*", "", $2); print $2 }'
              10.0.2.15
              fe80::a00:27ff:fe45:b085




              $ ip addr show dev "$default_iface" | awk '$1 == "inet" { sub("/.*", "", $2); print $2 }'
              10.0.2.15




              $ ip addr show dev "$default_iface" | awk '$1 == "inet6" { sub("/.*", "", $2); print $2 }'
              fe80::a00:27ff:fe45:b085





              share|improve this answer






























                5














                Here's another slightly terser method using procfs (assumes you're using Linux):



                default_iface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
                ip addr show dev "$default_iface" | awk '$1 ~ /^inet/ { sub("/.*", "", $2); print $2 }'


                This returns both the IPv4 and (if available) the IPv6 address of the interface. You can change the test if you only want one or the other (look for inet for IPv4, and inet6 for IPv6).





                $ default_iface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
                $ ip addr show dev "$default_iface" | awk '$1 ~ /^inet/ { sub("/.*", "", $2); print $2 }'
                10.0.2.15
                fe80::a00:27ff:fe45:b085




                $ ip addr show dev "$default_iface" | awk '$1 == "inet" { sub("/.*", "", $2); print $2 }'
                10.0.2.15




                $ ip addr show dev "$default_iface" | awk '$1 == "inet6" { sub("/.*", "", $2); print $2 }'
                fe80::a00:27ff:fe45:b085





                share|improve this answer




























                  5












                  5








                  5







                  Here's another slightly terser method using procfs (assumes you're using Linux):



                  default_iface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
                  ip addr show dev "$default_iface" | awk '$1 ~ /^inet/ { sub("/.*", "", $2); print $2 }'


                  This returns both the IPv4 and (if available) the IPv6 address of the interface. You can change the test if you only want one or the other (look for inet for IPv4, and inet6 for IPv6).





                  $ default_iface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
                  $ ip addr show dev "$default_iface" | awk '$1 ~ /^inet/ { sub("/.*", "", $2); print $2 }'
                  10.0.2.15
                  fe80::a00:27ff:fe45:b085




                  $ ip addr show dev "$default_iface" | awk '$1 == "inet" { sub("/.*", "", $2); print $2 }'
                  10.0.2.15




                  $ ip addr show dev "$default_iface" | awk '$1 == "inet6" { sub("/.*", "", $2); print $2 }'
                  fe80::a00:27ff:fe45:b085





                  share|improve this answer















                  Here's another slightly terser method using procfs (assumes you're using Linux):



                  default_iface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
                  ip addr show dev "$default_iface" | awk '$1 ~ /^inet/ { sub("/.*", "", $2); print $2 }'


                  This returns both the IPv4 and (if available) the IPv6 address of the interface. You can change the test if you only want one or the other (look for inet for IPv4, and inet6 for IPv6).





                  $ default_iface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
                  $ ip addr show dev "$default_iface" | awk '$1 ~ /^inet/ { sub("/.*", "", $2); print $2 }'
                  10.0.2.15
                  fe80::a00:27ff:fe45:b085




                  $ ip addr show dev "$default_iface" | awk '$1 == "inet" { sub("/.*", "", $2); print $2 }'
                  10.0.2.15




                  $ ip addr show dev "$default_iface" | awk '$1 == "inet6" { sub("/.*", "", $2); print $2 }'
                  fe80::a00:27ff:fe45:b085






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 9 '14 at 18:56

























                  answered Nov 9 '14 at 18:51









                  Chris DownChris Down

                  80k14189202




                  80k14189202

























                      1














                      Here's what I wrote:




                      1. Get the default interface from the "route" command.


                      It will print out which interface is the "default". For my host, I need to get the last column of the default line.



                      [root@pppdc9prd3ga mdesales]# route
                      Kernel IP routing table
                      Destination Gateway Genmask Flags Metric Ref Use Iface
                      192.168.4.0 * 255.255.252.0 U 0 0 0 bridge0
                      10.132.60.0 * 255.255.252.0 U 0 0 0 eth4
                      link-local * 255.255.0.0 U 1002 0 0 eth4
                      link-local * 255.255.0.0 U 1003 0 0 bridge0
                      default 10.132.60.1 0.0.0.0 UG 0 0 0 eth4



                      1. Use "ifconfig" to retrieve the IP address of that interface.


                      Just getting the addr: value.



                      [root@pppdc9prd3ga mdesales]# ifconfig eth4
                      eth4 Link encap:Ethernet HWaddr 00:50:56:01:42:91
                      inet addr:10.132.63.191 Bcast:10.132.63.255 Mask:255.255.252.0
                      UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
                      RX packets:1346288 errors:0 dropped:0 overruns:0 frame:0
                      TX packets:438844 errors:0 dropped:0 overruns:0 carrier:0
                      collisions:0 txqueuelen:1000
                      RX bytes:276243478 (263.4 MiB) TX bytes:116188062 (110.8 MiB)


                      So here's the script I came up with.



                      /app/myPublicIp.sh 
                      defaultInterface=$(route | grep default | awk '{print $(NF)}')
                      ifconfig $defaultInterface | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'


                      Here's it executing:



                      /app/ipFor.sh 
                      10.132.63.191


                      Hope it helps!






                      share|improve this answer
























                      • Nice, +1. You could simplify (well, shorten it anyway) that to ifconfig $(route | grep -oP '^default.*s+K.*') | grep -oP 'inet addr:K[^s]+')

                        – terdon
                        Nov 9 '14 at 18:15











                      • If you're going to do this on Linux, you might consider using procfs instead of parsing :-)

                        – Chris Down
                        Nov 9 '14 at 18:26













                      • @ChrisDown... would that be via /proc/net? Yeah, this is for a Docker cluster.

                        – Marcello de Sales
                        Nov 9 '14 at 18:39













                      • @MarcellodeSales Yeah, /proc/net/route. I've posted an answer to that effect.

                        – Chris Down
                        Nov 9 '14 at 18:51
















                      1














                      Here's what I wrote:




                      1. Get the default interface from the "route" command.


                      It will print out which interface is the "default". For my host, I need to get the last column of the default line.



                      [root@pppdc9prd3ga mdesales]# route
                      Kernel IP routing table
                      Destination Gateway Genmask Flags Metric Ref Use Iface
                      192.168.4.0 * 255.255.252.0 U 0 0 0 bridge0
                      10.132.60.0 * 255.255.252.0 U 0 0 0 eth4
                      link-local * 255.255.0.0 U 1002 0 0 eth4
                      link-local * 255.255.0.0 U 1003 0 0 bridge0
                      default 10.132.60.1 0.0.0.0 UG 0 0 0 eth4



                      1. Use "ifconfig" to retrieve the IP address of that interface.


                      Just getting the addr: value.



                      [root@pppdc9prd3ga mdesales]# ifconfig eth4
                      eth4 Link encap:Ethernet HWaddr 00:50:56:01:42:91
                      inet addr:10.132.63.191 Bcast:10.132.63.255 Mask:255.255.252.0
                      UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
                      RX packets:1346288 errors:0 dropped:0 overruns:0 frame:0
                      TX packets:438844 errors:0 dropped:0 overruns:0 carrier:0
                      collisions:0 txqueuelen:1000
                      RX bytes:276243478 (263.4 MiB) TX bytes:116188062 (110.8 MiB)


                      So here's the script I came up with.



                      /app/myPublicIp.sh 
                      defaultInterface=$(route | grep default | awk '{print $(NF)}')
                      ifconfig $defaultInterface | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'


                      Here's it executing:



                      /app/ipFor.sh 
                      10.132.63.191


                      Hope it helps!






                      share|improve this answer
























                      • Nice, +1. You could simplify (well, shorten it anyway) that to ifconfig $(route | grep -oP '^default.*s+K.*') | grep -oP 'inet addr:K[^s]+')

                        – terdon
                        Nov 9 '14 at 18:15











                      • If you're going to do this on Linux, you might consider using procfs instead of parsing :-)

                        – Chris Down
                        Nov 9 '14 at 18:26













                      • @ChrisDown... would that be via /proc/net? Yeah, this is for a Docker cluster.

                        – Marcello de Sales
                        Nov 9 '14 at 18:39













                      • @MarcellodeSales Yeah, /proc/net/route. I've posted an answer to that effect.

                        – Chris Down
                        Nov 9 '14 at 18:51














                      1












                      1








                      1







                      Here's what I wrote:




                      1. Get the default interface from the "route" command.


                      It will print out which interface is the "default". For my host, I need to get the last column of the default line.



                      [root@pppdc9prd3ga mdesales]# route
                      Kernel IP routing table
                      Destination Gateway Genmask Flags Metric Ref Use Iface
                      192.168.4.0 * 255.255.252.0 U 0 0 0 bridge0
                      10.132.60.0 * 255.255.252.0 U 0 0 0 eth4
                      link-local * 255.255.0.0 U 1002 0 0 eth4
                      link-local * 255.255.0.0 U 1003 0 0 bridge0
                      default 10.132.60.1 0.0.0.0 UG 0 0 0 eth4



                      1. Use "ifconfig" to retrieve the IP address of that interface.


                      Just getting the addr: value.



                      [root@pppdc9prd3ga mdesales]# ifconfig eth4
                      eth4 Link encap:Ethernet HWaddr 00:50:56:01:42:91
                      inet addr:10.132.63.191 Bcast:10.132.63.255 Mask:255.255.252.0
                      UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
                      RX packets:1346288 errors:0 dropped:0 overruns:0 frame:0
                      TX packets:438844 errors:0 dropped:0 overruns:0 carrier:0
                      collisions:0 txqueuelen:1000
                      RX bytes:276243478 (263.4 MiB) TX bytes:116188062 (110.8 MiB)


                      So here's the script I came up with.



                      /app/myPublicIp.sh 
                      defaultInterface=$(route | grep default | awk '{print $(NF)}')
                      ifconfig $defaultInterface | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'


                      Here's it executing:



                      /app/ipFor.sh 
                      10.132.63.191


                      Hope it helps!






                      share|improve this answer













                      Here's what I wrote:




                      1. Get the default interface from the "route" command.


                      It will print out which interface is the "default". For my host, I need to get the last column of the default line.



                      [root@pppdc9prd3ga mdesales]# route
                      Kernel IP routing table
                      Destination Gateway Genmask Flags Metric Ref Use Iface
                      192.168.4.0 * 255.255.252.0 U 0 0 0 bridge0
                      10.132.60.0 * 255.255.252.0 U 0 0 0 eth4
                      link-local * 255.255.0.0 U 1002 0 0 eth4
                      link-local * 255.255.0.0 U 1003 0 0 bridge0
                      default 10.132.60.1 0.0.0.0 UG 0 0 0 eth4



                      1. Use "ifconfig" to retrieve the IP address of that interface.


                      Just getting the addr: value.



                      [root@pppdc9prd3ga mdesales]# ifconfig eth4
                      eth4 Link encap:Ethernet HWaddr 00:50:56:01:42:91
                      inet addr:10.132.63.191 Bcast:10.132.63.255 Mask:255.255.252.0
                      UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
                      RX packets:1346288 errors:0 dropped:0 overruns:0 frame:0
                      TX packets:438844 errors:0 dropped:0 overruns:0 carrier:0
                      collisions:0 txqueuelen:1000
                      RX bytes:276243478 (263.4 MiB) TX bytes:116188062 (110.8 MiB)


                      So here's the script I came up with.



                      /app/myPublicIp.sh 
                      defaultInterface=$(route | grep default | awk '{print $(NF)}')
                      ifconfig $defaultInterface | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'


                      Here's it executing:



                      /app/ipFor.sh 
                      10.132.63.191


                      Hope it helps!







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 9 '14 at 18:09









                      Marcello de SalesMarcello de Sales

                      14816




                      14816













                      • Nice, +1. You could simplify (well, shorten it anyway) that to ifconfig $(route | grep -oP '^default.*s+K.*') | grep -oP 'inet addr:K[^s]+')

                        – terdon
                        Nov 9 '14 at 18:15











                      • If you're going to do this on Linux, you might consider using procfs instead of parsing :-)

                        – Chris Down
                        Nov 9 '14 at 18:26













                      • @ChrisDown... would that be via /proc/net? Yeah, this is for a Docker cluster.

                        – Marcello de Sales
                        Nov 9 '14 at 18:39













                      • @MarcellodeSales Yeah, /proc/net/route. I've posted an answer to that effect.

                        – Chris Down
                        Nov 9 '14 at 18:51



















                      • Nice, +1. You could simplify (well, shorten it anyway) that to ifconfig $(route | grep -oP '^default.*s+K.*') | grep -oP 'inet addr:K[^s]+')

                        – terdon
                        Nov 9 '14 at 18:15











                      • If you're going to do this on Linux, you might consider using procfs instead of parsing :-)

                        – Chris Down
                        Nov 9 '14 at 18:26













                      • @ChrisDown... would that be via /proc/net? Yeah, this is for a Docker cluster.

                        – Marcello de Sales
                        Nov 9 '14 at 18:39













                      • @MarcellodeSales Yeah, /proc/net/route. I've posted an answer to that effect.

                        – Chris Down
                        Nov 9 '14 at 18:51

















                      Nice, +1. You could simplify (well, shorten it anyway) that to ifconfig $(route | grep -oP '^default.*s+K.*') | grep -oP 'inet addr:K[^s]+')

                      – terdon
                      Nov 9 '14 at 18:15





                      Nice, +1. You could simplify (well, shorten it anyway) that to ifconfig $(route | grep -oP '^default.*s+K.*') | grep -oP 'inet addr:K[^s]+')

                      – terdon
                      Nov 9 '14 at 18:15













                      If you're going to do this on Linux, you might consider using procfs instead of parsing :-)

                      – Chris Down
                      Nov 9 '14 at 18:26







                      If you're going to do this on Linux, you might consider using procfs instead of parsing :-)

                      – Chris Down
                      Nov 9 '14 at 18:26















                      @ChrisDown... would that be via /proc/net? Yeah, this is for a Docker cluster.

                      – Marcello de Sales
                      Nov 9 '14 at 18:39







                      @ChrisDown... would that be via /proc/net? Yeah, this is for a Docker cluster.

                      – Marcello de Sales
                      Nov 9 '14 at 18:39















                      @MarcellodeSales Yeah, /proc/net/route. I've posted an answer to that effect.

                      – Chris Down
                      Nov 9 '14 at 18:51





                      @MarcellodeSales Yeah, /proc/net/route. I've posted an answer to that effect.

                      – Chris Down
                      Nov 9 '14 at 18:51











                      1














                      Lots of good answers here, but wanted to throw in my usual approach:



                      The simplest solution is to get the route for a public internet address:



                      $ ip route get 1.1.1.1 | grep -oP 'src KS+'
                      192.168.0.20


                      Another solution is to get the default gateway, and then get the IP addr used to communicate with that gateway:



                      $ ip route get $(ip route show 0.0.0.0/0 | grep -oP 'via KS+') | grep -oP 'src KS+'
                      192.168.0.20





                      share|improve this answer




























                        1














                        Lots of good answers here, but wanted to throw in my usual approach:



                        The simplest solution is to get the route for a public internet address:



                        $ ip route get 1.1.1.1 | grep -oP 'src KS+'
                        192.168.0.20


                        Another solution is to get the default gateway, and then get the IP addr used to communicate with that gateway:



                        $ ip route get $(ip route show 0.0.0.0/0 | grep -oP 'via KS+') | grep -oP 'src KS+'
                        192.168.0.20





                        share|improve this answer


























                          1












                          1








                          1







                          Lots of good answers here, but wanted to throw in my usual approach:



                          The simplest solution is to get the route for a public internet address:



                          $ ip route get 1.1.1.1 | grep -oP 'src KS+'
                          192.168.0.20


                          Another solution is to get the default gateway, and then get the IP addr used to communicate with that gateway:



                          $ ip route get $(ip route show 0.0.0.0/0 | grep -oP 'via KS+') | grep -oP 'src KS+'
                          192.168.0.20





                          share|improve this answer













                          Lots of good answers here, but wanted to throw in my usual approach:



                          The simplest solution is to get the route for a public internet address:



                          $ ip route get 1.1.1.1 | grep -oP 'src KS+'
                          192.168.0.20


                          Another solution is to get the default gateway, and then get the IP addr used to communicate with that gateway:



                          $ ip route get $(ip route show 0.0.0.0/0 | grep -oP 'via KS+') | grep -oP 'src KS+'
                          192.168.0.20






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 10 '14 at 0:02









                          PatrickPatrick

                          50.3k11128180




                          50.3k11128180























                              0














                              If what you want is the IP address assigned to the default interface (which is what I understood from the comments under the question), using the Swiss army knife of network setup (ip) should be enough:



                              $ ip route | grep '^default'
                              default via 10.176.143.1 dev eth1 metric 203
                              $ ip addr show eth1
                              4: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
                              link/ether c0:de:f1:72:30:48 brd ff:ff:ff:ff:ff:ff
                              inet 10.176.143.71/24 brd 10.176.143.255 scope global eth1
                              valid_lft forever preferred_lft forever


                              This shortens to



                              $ ip addr show 
                              $( /sbin/ip route
                              | grep '^default'
                              | sed 's/^.*dev ([^ t]*) .*$/1/' )


                              which is ugly, because it is parsing something that probably wasn't really meant to be parsed (output of ip route), but should work.






                              share|improve this answer




























                                0














                                If what you want is the IP address assigned to the default interface (which is what I understood from the comments under the question), using the Swiss army knife of network setup (ip) should be enough:



                                $ ip route | grep '^default'
                                default via 10.176.143.1 dev eth1 metric 203
                                $ ip addr show eth1
                                4: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
                                link/ether c0:de:f1:72:30:48 brd ff:ff:ff:ff:ff:ff
                                inet 10.176.143.71/24 brd 10.176.143.255 scope global eth1
                                valid_lft forever preferred_lft forever


                                This shortens to



                                $ ip addr show 
                                $( /sbin/ip route
                                | grep '^default'
                                | sed 's/^.*dev ([^ t]*) .*$/1/' )


                                which is ugly, because it is parsing something that probably wasn't really meant to be parsed (output of ip route), but should work.






                                share|improve this answer


























                                  0












                                  0








                                  0







                                  If what you want is the IP address assigned to the default interface (which is what I understood from the comments under the question), using the Swiss army knife of network setup (ip) should be enough:



                                  $ ip route | grep '^default'
                                  default via 10.176.143.1 dev eth1 metric 203
                                  $ ip addr show eth1
                                  4: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
                                  link/ether c0:de:f1:72:30:48 brd ff:ff:ff:ff:ff:ff
                                  inet 10.176.143.71/24 brd 10.176.143.255 scope global eth1
                                  valid_lft forever preferred_lft forever


                                  This shortens to



                                  $ ip addr show 
                                  $( /sbin/ip route
                                  | grep '^default'
                                  | sed 's/^.*dev ([^ t]*) .*$/1/' )


                                  which is ugly, because it is parsing something that probably wasn't really meant to be parsed (output of ip route), but should work.






                                  share|improve this answer













                                  If what you want is the IP address assigned to the default interface (which is what I understood from the comments under the question), using the Swiss army knife of network setup (ip) should be enough:



                                  $ ip route | grep '^default'
                                  default via 10.176.143.1 dev eth1 metric 203
                                  $ ip addr show eth1
                                  4: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
                                  link/ether c0:de:f1:72:30:48 brd ff:ff:ff:ff:ff:ff
                                  inet 10.176.143.71/24 brd 10.176.143.255 scope global eth1
                                  valid_lft forever preferred_lft forever


                                  This shortens to



                                  $ ip addr show 
                                  $( /sbin/ip route
                                  | grep '^default'
                                  | sed 's/^.*dev ([^ t]*) .*$/1/' )


                                  which is ugly, because it is parsing something that probably wasn't really meant to be parsed (output of ip route), but should work.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Nov 9 '14 at 21:59









                                  peterphpeterph

                                  23.5k24457




                                  23.5k24457























                                      0














                                      My favorite one is following.



                                      Get the default interface:



                                      $ ip r | grep -oP 'default .* K.+'
                                      eth0


                                      Get the ip of an interface:



                                      $ ip a show eth0 | grep -oP 'inet K[d.]+'
                                      10.33.44.135


                                      Combined:



                                      $ ip a show $(ip r | grep -oP 'default .* K.+') | grep -oP 'inet K[d.]+'
                                      10.33.44.135





                                      share|improve this answer




























                                        0














                                        My favorite one is following.



                                        Get the default interface:



                                        $ ip r | grep -oP 'default .* K.+'
                                        eth0


                                        Get the ip of an interface:



                                        $ ip a show eth0 | grep -oP 'inet K[d.]+'
                                        10.33.44.135


                                        Combined:



                                        $ ip a show $(ip r | grep -oP 'default .* K.+') | grep -oP 'inet K[d.]+'
                                        10.33.44.135





                                        share|improve this answer


























                                          0












                                          0








                                          0







                                          My favorite one is following.



                                          Get the default interface:



                                          $ ip r | grep -oP 'default .* K.+'
                                          eth0


                                          Get the ip of an interface:



                                          $ ip a show eth0 | grep -oP 'inet K[d.]+'
                                          10.33.44.135


                                          Combined:



                                          $ ip a show $(ip r | grep -oP 'default .* K.+') | grep -oP 'inet K[d.]+'
                                          10.33.44.135





                                          share|improve this answer













                                          My favorite one is following.



                                          Get the default interface:



                                          $ ip r | grep -oP 'default .* K.+'
                                          eth0


                                          Get the ip of an interface:



                                          $ ip a show eth0 | grep -oP 'inet K[d.]+'
                                          10.33.44.135


                                          Combined:



                                          $ ip a show $(ip r | grep -oP 'default .* K.+') | grep -oP 'inet K[d.]+'
                                          10.33.44.135






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Aug 12 '18 at 17:11









                                          AshaldAshald

                                          1




                                          1























                                              0














                                              Simple Command with default interface.



                                              ip route | grep src | awk '{print $NF; exit}'


                                              Tested on All Unix OS






                                              share|improve this answer






























                                                0














                                                Simple Command with default interface.



                                                ip route | grep src | awk '{print $NF; exit}'


                                                Tested on All Unix OS






                                                share|improve this answer




























                                                  0












                                                  0








                                                  0







                                                  Simple Command with default interface.



                                                  ip route | grep src | awk '{print $NF; exit}'


                                                  Tested on All Unix OS






                                                  share|improve this answer















                                                  Simple Command with default interface.



                                                  ip route | grep src | awk '{print $NF; exit}'


                                                  Tested on All Unix OS







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Dec 12 '18 at 20:51

























                                                  answered Dec 2 '18 at 19:29









                                                  M.S.ArunM.S.Arun

                                                  1397




                                                  1397























                                                      0














                                                      This worked for me on Centos 7. Find default interface using ip



                                                      ifconfig $(ip route | awk '/default/ { print $5 }') | grep "inet " | awk '{print $2}'





                                                      share|improve this answer








                                                      New contributor




                                                      N A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                      Check out our Code of Conduct.

























                                                        0














                                                        This worked for me on Centos 7. Find default interface using ip



                                                        ifconfig $(ip route | awk '/default/ { print $5 }') | grep "inet " | awk '{print $2}'





                                                        share|improve this answer








                                                        New contributor




                                                        N A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                        Check out our Code of Conduct.























                                                          0












                                                          0








                                                          0







                                                          This worked for me on Centos 7. Find default interface using ip



                                                          ifconfig $(ip route | awk '/default/ { print $5 }') | grep "inet " | awk '{print $2}'





                                                          share|improve this answer








                                                          New contributor




                                                          N A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                          Check out our Code of Conduct.










                                                          This worked for me on Centos 7. Find default interface using ip



                                                          ifconfig $(ip route | awk '/default/ { print $5 }') | grep "inet " | awk '{print $2}'






                                                          share|improve this answer








                                                          New contributor




                                                          N A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                          Check out our Code of Conduct.









                                                          share|improve this answer



                                                          share|improve this answer






                                                          New contributor




                                                          N A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                          Check out our Code of Conduct.









                                                          answered 15 mins ago









                                                          N AN A

                                                          1012




                                                          1012




                                                          New contributor




                                                          N A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                          Check out our Code of Conduct.





                                                          New contributor





                                                          N A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                          Check out our Code of Conduct.






                                                          N A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                          Check out our Code of Conduct.






























                                                              draft saved

                                                              draft discarded




















































                                                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                                              • 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%2funix.stackexchange.com%2fquestions%2f166999%2fhow-to-display-the-ip-address-of-the-default-interface-with-internet-connection%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 rewrite equation of hyperbola in standard form

                                                              No ethernet ip address in my vocore2