How to capture the first IP address from a ifconfig command?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
9
down vote

favorite












How to capture the first IP address that comes from ifconfig command?



ifconfig -a
enw178032: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 100.14.22.12 netmask 255.255.0.0 broadcast 100.14.255.255
inet6 fe80::250:56ff:fe9c:158a prefixlen 64 scopeid 0x20<link>
ether 00:10:56:9c:65:8a txqueuelen 1000 (Ethernet)
RX packets 26846250 bytes 12068811576 (11.2 GiB)
RX errors 0 dropped 58671 overruns 0 frame 0
TX packets 3368855 bytes 1139160934 (1.0 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


Expected result:



IP=100.14.22.12






share|improve this question


















  • 3




    The question has been answered, either using awk or grep / cut, etc. But it's still a bad idea in general to use and grep from ifconfig. ip is better suited and better supported in modern Linux builds.
    – Pedro
    Feb 27 at 9:35






  • 1




    This sounds like an X-Y problem. What information do you really want? The main network-facing IP address of the machine? Any network-facing IP address of the machine? The IP address of the first network adapter? Any IP address, doesn't matter what it is? There is probably a more direct, accurate, and portable way to get the information you need.
    – alex.forencich
    Feb 27 at 23:08














up vote
9
down vote

favorite












How to capture the first IP address that comes from ifconfig command?



ifconfig -a
enw178032: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 100.14.22.12 netmask 255.255.0.0 broadcast 100.14.255.255
inet6 fe80::250:56ff:fe9c:158a prefixlen 64 scopeid 0x20<link>
ether 00:10:56:9c:65:8a txqueuelen 1000 (Ethernet)
RX packets 26846250 bytes 12068811576 (11.2 GiB)
RX errors 0 dropped 58671 overruns 0 frame 0
TX packets 3368855 bytes 1139160934 (1.0 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


Expected result:



IP=100.14.22.12






share|improve this question


















  • 3




    The question has been answered, either using awk or grep / cut, etc. But it's still a bad idea in general to use and grep from ifconfig. ip is better suited and better supported in modern Linux builds.
    – Pedro
    Feb 27 at 9:35






  • 1




    This sounds like an X-Y problem. What information do you really want? The main network-facing IP address of the machine? Any network-facing IP address of the machine? The IP address of the first network adapter? Any IP address, doesn't matter what it is? There is probably a more direct, accurate, and portable way to get the information you need.
    – alex.forencich
    Feb 27 at 23:08












up vote
9
down vote

favorite









up vote
9
down vote

favorite











How to capture the first IP address that comes from ifconfig command?



ifconfig -a
enw178032: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 100.14.22.12 netmask 255.255.0.0 broadcast 100.14.255.255
inet6 fe80::250:56ff:fe9c:158a prefixlen 64 scopeid 0x20<link>
ether 00:10:56:9c:65:8a txqueuelen 1000 (Ethernet)
RX packets 26846250 bytes 12068811576 (11.2 GiB)
RX errors 0 dropped 58671 overruns 0 frame 0
TX packets 3368855 bytes 1139160934 (1.0 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


Expected result:



IP=100.14.22.12






share|improve this question














How to capture the first IP address that comes from ifconfig command?



ifconfig -a
enw178032: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 100.14.22.12 netmask 255.255.0.0 broadcast 100.14.255.255
inet6 fe80::250:56ff:fe9c:158a prefixlen 64 scopeid 0x20<link>
ether 00:10:56:9c:65:8a txqueuelen 1000 (Ethernet)
RX packets 26846250 bytes 12068811576 (11.2 GiB)
RX errors 0 dropped 58671 overruns 0 frame 0
TX packets 3368855 bytes 1139160934 (1.0 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


Expected result:



IP=100.14.22.12








share|improve this question













share|improve this question




share|improve this question








edited Feb 27 at 20:12









Rui F Ribeiro

34.9k1269113




34.9k1269113










asked Feb 27 at 9:12









yael

1,9551145




1,9551145







  • 3




    The question has been answered, either using awk or grep / cut, etc. But it's still a bad idea in general to use and grep from ifconfig. ip is better suited and better supported in modern Linux builds.
    – Pedro
    Feb 27 at 9:35






  • 1




    This sounds like an X-Y problem. What information do you really want? The main network-facing IP address of the machine? Any network-facing IP address of the machine? The IP address of the first network adapter? Any IP address, doesn't matter what it is? There is probably a more direct, accurate, and portable way to get the information you need.
    – alex.forencich
    Feb 27 at 23:08












  • 3




    The question has been answered, either using awk or grep / cut, etc. But it's still a bad idea in general to use and grep from ifconfig. ip is better suited and better supported in modern Linux builds.
    – Pedro
    Feb 27 at 9:35






  • 1




    This sounds like an X-Y problem. What information do you really want? The main network-facing IP address of the machine? Any network-facing IP address of the machine? The IP address of the first network adapter? Any IP address, doesn't matter what it is? There is probably a more direct, accurate, and portable way to get the information you need.
    – alex.forencich
    Feb 27 at 23:08







3




3




The question has been answered, either using awk or grep / cut, etc. But it's still a bad idea in general to use and grep from ifconfig. ip is better suited and better supported in modern Linux builds.
– Pedro
Feb 27 at 9:35




The question has been answered, either using awk or grep / cut, etc. But it's still a bad idea in general to use and grep from ifconfig. ip is better suited and better supported in modern Linux builds.
– Pedro
Feb 27 at 9:35




1




1




This sounds like an X-Y problem. What information do you really want? The main network-facing IP address of the machine? Any network-facing IP address of the machine? The IP address of the first network adapter? Any IP address, doesn't matter what it is? There is probably a more direct, accurate, and portable way to get the information you need.
– alex.forencich
Feb 27 at 23:08




This sounds like an X-Y problem. What information do you really want? The main network-facing IP address of the machine? Any network-facing IP address of the machine? The IP address of the first network adapter? Any IP address, doesn't matter what it is? There is probably a more direct, accurate, and portable way to get the information you need.
– alex.forencich
Feb 27 at 23:08










3 Answers
3






active

oldest

votes

















up vote
18
down vote



accepted










It is better avoid using ifconfig for getting an IP address in a scriptas it is deprecated in some distributions (e.g. CentOS and others, do not install it by default anymore).



In others systems, the output of ifconfig varies according to the release of the distribution (e.g. the output/spacing/fields of ifconfig differs from Debian 8 to Debian 9, for instance).



For getting the IP address with ip, in a similar way you are asking:



ip addr | awk ' !/127.0.0.1/ && /inet/ gsub(//.*/, "", $2); print "IP="$2 '


Or better yet:



$ ip -o -4 address show | awk ' NR==2 gsub(//.*/, "", $4); print $4 '
192.168.1.249


Or, as you ask "IP="



#!/bin/bash
echo -n "IP="
ip -o -4 address show | awk ' NR==2 gsub(//.*/, "", $4); print $4 '


Adapting shamelessly the idea from @Roman



$ ip -o -4 address show | awk ' NR==2 gsub(//.*/, "", $4); print "IP="$4 ' 
IP=192.168.1.249


Normal output:



 $ ip -o -4 address show 
1: lo inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever
2: eth0 inet 192.168.1.249/24 brd 192.168.1.255 scope global eth0 valid_lft forever preferred_lft forever


From man ip:




-o, -oneline

output each record on a single line, replacing line feeds with
the '' character. This is convenient when you want to count
records with wc(1) or to grep(1) the output.




See one example of why ifconfig is not advised: BBB: `bbb-conf --check` showing IP addresses as `inet` - ifconfig woes



For understanding why ifconfig is on the way out, see Difference between 'ifconfig' and 'ip' commands




ifconfig is from net-tools, which hasn't been able to fully keep up
with the Linux network stack for a long time. It also still uses ioctl
for network configuration, which is an ugly and less powerful way of
interacting with the kernel.



Around 2005 a new mechanism for controlling the network stack was
introduced - netlink sockets.



To configure the network interface iproute2 makes use of that
full-duplex netlink socket mechanism, while ifconfig relies on an
ioctl system call.







share|improve this answer






















  • I prefer not to set eth0 as default because this name changing on machines , can we provide flexible syntax
    – yael
    Feb 27 at 9:31










  • @yael Changed, took out eth0. However Roman did better than me printing the "IP=" inside awk. Too much brainpower for me, still waking up. Avoid ifconfig, it has not future, and the position of IP address changes, there are at least two different versions/implementations(?) for Linux out there that I know off.
    – Rui F Ribeiro
    Feb 27 at 9:35











  • Just for info, on newer versions of iproute2 (eg: not on CentOS7 nor Debian8) ip can take a -brief parameter and its output becomes easier to parse (2015-08-31: git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/… )
    – A.B
    Feb 27 at 12:18











  • @A.B also -o as I use helps, thanks, will check it ou
    – Rui F Ribeiro
    Feb 27 at 12:40






  • 1




    Ah, I didn't know about -o which indeed seems useful for scripts
    – A.B
    Feb 27 at 13:37


















up vote
6
down vote













Awk solution:



ifconfig -a | awk 'NR==2 sub(/^[^0-9]*/, "", $2); printf "IP=%sn", $2; exit '


Sample output:



IP=10.0.2.15





share|improve this answer





























    up vote
    -3
    down vote













    ip addr | grep -v 127.0.0.1 | grep 'inet ' | 
    awk 'print $2' | awk -F "/" 'print "IP="$1'





    share|improve this answer


















    • 1




      see this: ip addr | awk ' !/127.0.0.1/ && /inet/ gsub(//.*/, "", $2); print "IP="$2 '
      – Rui F Ribeiro
      Feb 27 at 19:25






    • 1




      Why are you grepping for the localhost IP 127.0.0.1?
      – Jeff Schaller
      Feb 27 at 20:12










    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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f426883%2fhow-to-capture-the-first-ip-address-from-a-ifconfig-command%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    18
    down vote



    accepted










    It is better avoid using ifconfig for getting an IP address in a scriptas it is deprecated in some distributions (e.g. CentOS and others, do not install it by default anymore).



    In others systems, the output of ifconfig varies according to the release of the distribution (e.g. the output/spacing/fields of ifconfig differs from Debian 8 to Debian 9, for instance).



    For getting the IP address with ip, in a similar way you are asking:



    ip addr | awk ' !/127.0.0.1/ && /inet/ gsub(//.*/, "", $2); print "IP="$2 '


    Or better yet:



    $ ip -o -4 address show | awk ' NR==2 gsub(//.*/, "", $4); print $4 '
    192.168.1.249


    Or, as you ask "IP="



    #!/bin/bash
    echo -n "IP="
    ip -o -4 address show | awk ' NR==2 gsub(//.*/, "", $4); print $4 '


    Adapting shamelessly the idea from @Roman



    $ ip -o -4 address show | awk ' NR==2 gsub(//.*/, "", $4); print "IP="$4 ' 
    IP=192.168.1.249


    Normal output:



     $ ip -o -4 address show 
    1: lo inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever
    2: eth0 inet 192.168.1.249/24 brd 192.168.1.255 scope global eth0 valid_lft forever preferred_lft forever


    From man ip:




    -o, -oneline

    output each record on a single line, replacing line feeds with
    the '' character. This is convenient when you want to count
    records with wc(1) or to grep(1) the output.




    See one example of why ifconfig is not advised: BBB: `bbb-conf --check` showing IP addresses as `inet` - ifconfig woes



    For understanding why ifconfig is on the way out, see Difference between 'ifconfig' and 'ip' commands




    ifconfig is from net-tools, which hasn't been able to fully keep up
    with the Linux network stack for a long time. It also still uses ioctl
    for network configuration, which is an ugly and less powerful way of
    interacting with the kernel.



    Around 2005 a new mechanism for controlling the network stack was
    introduced - netlink sockets.



    To configure the network interface iproute2 makes use of that
    full-duplex netlink socket mechanism, while ifconfig relies on an
    ioctl system call.







    share|improve this answer






















    • I prefer not to set eth0 as default because this name changing on machines , can we provide flexible syntax
      – yael
      Feb 27 at 9:31










    • @yael Changed, took out eth0. However Roman did better than me printing the "IP=" inside awk. Too much brainpower for me, still waking up. Avoid ifconfig, it has not future, and the position of IP address changes, there are at least two different versions/implementations(?) for Linux out there that I know off.
      – Rui F Ribeiro
      Feb 27 at 9:35











    • Just for info, on newer versions of iproute2 (eg: not on CentOS7 nor Debian8) ip can take a -brief parameter and its output becomes easier to parse (2015-08-31: git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/… )
      – A.B
      Feb 27 at 12:18











    • @A.B also -o as I use helps, thanks, will check it ou
      – Rui F Ribeiro
      Feb 27 at 12:40






    • 1




      Ah, I didn't know about -o which indeed seems useful for scripts
      – A.B
      Feb 27 at 13:37















    up vote
    18
    down vote



    accepted










    It is better avoid using ifconfig for getting an IP address in a scriptas it is deprecated in some distributions (e.g. CentOS and others, do not install it by default anymore).



    In others systems, the output of ifconfig varies according to the release of the distribution (e.g. the output/spacing/fields of ifconfig differs from Debian 8 to Debian 9, for instance).



    For getting the IP address with ip, in a similar way you are asking:



    ip addr | awk ' !/127.0.0.1/ && /inet/ gsub(//.*/, "", $2); print "IP="$2 '


    Or better yet:



    $ ip -o -4 address show | awk ' NR==2 gsub(//.*/, "", $4); print $4 '
    192.168.1.249


    Or, as you ask "IP="



    #!/bin/bash
    echo -n "IP="
    ip -o -4 address show | awk ' NR==2 gsub(//.*/, "", $4); print $4 '


    Adapting shamelessly the idea from @Roman



    $ ip -o -4 address show | awk ' NR==2 gsub(//.*/, "", $4); print "IP="$4 ' 
    IP=192.168.1.249


    Normal output:



     $ ip -o -4 address show 
    1: lo inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever
    2: eth0 inet 192.168.1.249/24 brd 192.168.1.255 scope global eth0 valid_lft forever preferred_lft forever


    From man ip:




    -o, -oneline

    output each record on a single line, replacing line feeds with
    the '' character. This is convenient when you want to count
    records with wc(1) or to grep(1) the output.




    See one example of why ifconfig is not advised: BBB: `bbb-conf --check` showing IP addresses as `inet` - ifconfig woes



    For understanding why ifconfig is on the way out, see Difference between 'ifconfig' and 'ip' commands




    ifconfig is from net-tools, which hasn't been able to fully keep up
    with the Linux network stack for a long time. It also still uses ioctl
    for network configuration, which is an ugly and less powerful way of
    interacting with the kernel.



    Around 2005 a new mechanism for controlling the network stack was
    introduced - netlink sockets.



    To configure the network interface iproute2 makes use of that
    full-duplex netlink socket mechanism, while ifconfig relies on an
    ioctl system call.







    share|improve this answer






















    • I prefer not to set eth0 as default because this name changing on machines , can we provide flexible syntax
      – yael
      Feb 27 at 9:31










    • @yael Changed, took out eth0. However Roman did better than me printing the "IP=" inside awk. Too much brainpower for me, still waking up. Avoid ifconfig, it has not future, and the position of IP address changes, there are at least two different versions/implementations(?) for Linux out there that I know off.
      – Rui F Ribeiro
      Feb 27 at 9:35











    • Just for info, on newer versions of iproute2 (eg: not on CentOS7 nor Debian8) ip can take a -brief parameter and its output becomes easier to parse (2015-08-31: git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/… )
      – A.B
      Feb 27 at 12:18











    • @A.B also -o as I use helps, thanks, will check it ou
      – Rui F Ribeiro
      Feb 27 at 12:40






    • 1




      Ah, I didn't know about -o which indeed seems useful for scripts
      – A.B
      Feb 27 at 13:37













    up vote
    18
    down vote



    accepted







    up vote
    18
    down vote



    accepted






    It is better avoid using ifconfig for getting an IP address in a scriptas it is deprecated in some distributions (e.g. CentOS and others, do not install it by default anymore).



    In others systems, the output of ifconfig varies according to the release of the distribution (e.g. the output/spacing/fields of ifconfig differs from Debian 8 to Debian 9, for instance).



    For getting the IP address with ip, in a similar way you are asking:



    ip addr | awk ' !/127.0.0.1/ && /inet/ gsub(//.*/, "", $2); print "IP="$2 '


    Or better yet:



    $ ip -o -4 address show | awk ' NR==2 gsub(//.*/, "", $4); print $4 '
    192.168.1.249


    Or, as you ask "IP="



    #!/bin/bash
    echo -n "IP="
    ip -o -4 address show | awk ' NR==2 gsub(//.*/, "", $4); print $4 '


    Adapting shamelessly the idea from @Roman



    $ ip -o -4 address show | awk ' NR==2 gsub(//.*/, "", $4); print "IP="$4 ' 
    IP=192.168.1.249


    Normal output:



     $ ip -o -4 address show 
    1: lo inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever
    2: eth0 inet 192.168.1.249/24 brd 192.168.1.255 scope global eth0 valid_lft forever preferred_lft forever


    From man ip:




    -o, -oneline

    output each record on a single line, replacing line feeds with
    the '' character. This is convenient when you want to count
    records with wc(1) or to grep(1) the output.




    See one example of why ifconfig is not advised: BBB: `bbb-conf --check` showing IP addresses as `inet` - ifconfig woes



    For understanding why ifconfig is on the way out, see Difference between 'ifconfig' and 'ip' commands




    ifconfig is from net-tools, which hasn't been able to fully keep up
    with the Linux network stack for a long time. It also still uses ioctl
    for network configuration, which is an ugly and less powerful way of
    interacting with the kernel.



    Around 2005 a new mechanism for controlling the network stack was
    introduced - netlink sockets.



    To configure the network interface iproute2 makes use of that
    full-duplex netlink socket mechanism, while ifconfig relies on an
    ioctl system call.







    share|improve this answer














    It is better avoid using ifconfig for getting an IP address in a scriptas it is deprecated in some distributions (e.g. CentOS and others, do not install it by default anymore).



    In others systems, the output of ifconfig varies according to the release of the distribution (e.g. the output/spacing/fields of ifconfig differs from Debian 8 to Debian 9, for instance).



    For getting the IP address with ip, in a similar way you are asking:



    ip addr | awk ' !/127.0.0.1/ && /inet/ gsub(//.*/, "", $2); print "IP="$2 '


    Or better yet:



    $ ip -o -4 address show | awk ' NR==2 gsub(//.*/, "", $4); print $4 '
    192.168.1.249


    Or, as you ask "IP="



    #!/bin/bash
    echo -n "IP="
    ip -o -4 address show | awk ' NR==2 gsub(//.*/, "", $4); print $4 '


    Adapting shamelessly the idea from @Roman



    $ ip -o -4 address show | awk ' NR==2 gsub(//.*/, "", $4); print "IP="$4 ' 
    IP=192.168.1.249


    Normal output:



     $ ip -o -4 address show 
    1: lo inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever
    2: eth0 inet 192.168.1.249/24 brd 192.168.1.255 scope global eth0 valid_lft forever preferred_lft forever


    From man ip:




    -o, -oneline

    output each record on a single line, replacing line feeds with
    the '' character. This is convenient when you want to count
    records with wc(1) or to grep(1) the output.




    See one example of why ifconfig is not advised: BBB: `bbb-conf --check` showing IP addresses as `inet` - ifconfig woes



    For understanding why ifconfig is on the way out, see Difference between 'ifconfig' and 'ip' commands




    ifconfig is from net-tools, which hasn't been able to fully keep up
    with the Linux network stack for a long time. It also still uses ioctl
    for network configuration, which is an ugly and less powerful way of
    interacting with the kernel.



    Around 2005 a new mechanism for controlling the network stack was
    introduced - netlink sockets.



    To configure the network interface iproute2 makes use of that
    full-duplex netlink socket mechanism, while ifconfig relies on an
    ioctl system call.








    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 28 at 10:18

























    answered Feb 27 at 9:27









    Rui F Ribeiro

    34.9k1269113




    34.9k1269113











    • I prefer not to set eth0 as default because this name changing on machines , can we provide flexible syntax
      – yael
      Feb 27 at 9:31










    • @yael Changed, took out eth0. However Roman did better than me printing the "IP=" inside awk. Too much brainpower for me, still waking up. Avoid ifconfig, it has not future, and the position of IP address changes, there are at least two different versions/implementations(?) for Linux out there that I know off.
      – Rui F Ribeiro
      Feb 27 at 9:35











    • Just for info, on newer versions of iproute2 (eg: not on CentOS7 nor Debian8) ip can take a -brief parameter and its output becomes easier to parse (2015-08-31: git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/… )
      – A.B
      Feb 27 at 12:18











    • @A.B also -o as I use helps, thanks, will check it ou
      – Rui F Ribeiro
      Feb 27 at 12:40






    • 1




      Ah, I didn't know about -o which indeed seems useful for scripts
      – A.B
      Feb 27 at 13:37

















    • I prefer not to set eth0 as default because this name changing on machines , can we provide flexible syntax
      – yael
      Feb 27 at 9:31










    • @yael Changed, took out eth0. However Roman did better than me printing the "IP=" inside awk. Too much brainpower for me, still waking up. Avoid ifconfig, it has not future, and the position of IP address changes, there are at least two different versions/implementations(?) for Linux out there that I know off.
      – Rui F Ribeiro
      Feb 27 at 9:35











    • Just for info, on newer versions of iproute2 (eg: not on CentOS7 nor Debian8) ip can take a -brief parameter and its output becomes easier to parse (2015-08-31: git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/… )
      – A.B
      Feb 27 at 12:18











    • @A.B also -o as I use helps, thanks, will check it ou
      – Rui F Ribeiro
      Feb 27 at 12:40






    • 1




      Ah, I didn't know about -o which indeed seems useful for scripts
      – A.B
      Feb 27 at 13:37
















    I prefer not to set eth0 as default because this name changing on machines , can we provide flexible syntax
    – yael
    Feb 27 at 9:31




    I prefer not to set eth0 as default because this name changing on machines , can we provide flexible syntax
    – yael
    Feb 27 at 9:31












    @yael Changed, took out eth0. However Roman did better than me printing the "IP=" inside awk. Too much brainpower for me, still waking up. Avoid ifconfig, it has not future, and the position of IP address changes, there are at least two different versions/implementations(?) for Linux out there that I know off.
    – Rui F Ribeiro
    Feb 27 at 9:35





    @yael Changed, took out eth0. However Roman did better than me printing the "IP=" inside awk. Too much brainpower for me, still waking up. Avoid ifconfig, it has not future, and the position of IP address changes, there are at least two different versions/implementations(?) for Linux out there that I know off.
    – Rui F Ribeiro
    Feb 27 at 9:35













    Just for info, on newer versions of iproute2 (eg: not on CentOS7 nor Debian8) ip can take a -brief parameter and its output becomes easier to parse (2015-08-31: git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/… )
    – A.B
    Feb 27 at 12:18





    Just for info, on newer versions of iproute2 (eg: not on CentOS7 nor Debian8) ip can take a -brief parameter and its output becomes easier to parse (2015-08-31: git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/… )
    – A.B
    Feb 27 at 12:18













    @A.B also -o as I use helps, thanks, will check it ou
    – Rui F Ribeiro
    Feb 27 at 12:40




    @A.B also -o as I use helps, thanks, will check it ou
    – Rui F Ribeiro
    Feb 27 at 12:40




    1




    1




    Ah, I didn't know about -o which indeed seems useful for scripts
    – A.B
    Feb 27 at 13:37





    Ah, I didn't know about -o which indeed seems useful for scripts
    – A.B
    Feb 27 at 13:37













    up vote
    6
    down vote













    Awk solution:



    ifconfig -a | awk 'NR==2 sub(/^[^0-9]*/, "", $2); printf "IP=%sn", $2; exit '


    Sample output:



    IP=10.0.2.15





    share|improve this answer


























      up vote
      6
      down vote













      Awk solution:



      ifconfig -a | awk 'NR==2 sub(/^[^0-9]*/, "", $2); printf "IP=%sn", $2; exit '


      Sample output:



      IP=10.0.2.15





      share|improve this answer
























        up vote
        6
        down vote










        up vote
        6
        down vote









        Awk solution:



        ifconfig -a | awk 'NR==2 sub(/^[^0-9]*/, "", $2); printf "IP=%sn", $2; exit '


        Sample output:



        IP=10.0.2.15





        share|improve this answer














        Awk solution:



        ifconfig -a | awk 'NR==2 sub(/^[^0-9]*/, "", $2); printf "IP=%sn", $2; exit '


        Sample output:



        IP=10.0.2.15






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 27 at 19:40

























        answered Feb 27 at 9:30









        RomanPerekhrest

        22.4k12144




        22.4k12144




















            up vote
            -3
            down vote













            ip addr | grep -v 127.0.0.1 | grep 'inet ' | 
            awk 'print $2' | awk -F "/" 'print "IP="$1'





            share|improve this answer


















            • 1




              see this: ip addr | awk ' !/127.0.0.1/ && /inet/ gsub(//.*/, "", $2); print "IP="$2 '
              – Rui F Ribeiro
              Feb 27 at 19:25






            • 1




              Why are you grepping for the localhost IP 127.0.0.1?
              – Jeff Schaller
              Feb 27 at 20:12














            up vote
            -3
            down vote













            ip addr | grep -v 127.0.0.1 | grep 'inet ' | 
            awk 'print $2' | awk -F "/" 'print "IP="$1'





            share|improve this answer


















            • 1




              see this: ip addr | awk ' !/127.0.0.1/ && /inet/ gsub(//.*/, "", $2); print "IP="$2 '
              – Rui F Ribeiro
              Feb 27 at 19:25






            • 1




              Why are you grepping for the localhost IP 127.0.0.1?
              – Jeff Schaller
              Feb 27 at 20:12












            up vote
            -3
            down vote










            up vote
            -3
            down vote









            ip addr | grep -v 127.0.0.1 | grep 'inet ' | 
            awk 'print $2' | awk -F "/" 'print "IP="$1'





            share|improve this answer














            ip addr | grep -v 127.0.0.1 | grep 'inet ' | 
            awk 'print $2' | awk -F "/" 'print "IP="$1'






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 27 at 20:46









            Rui F Ribeiro

            34.9k1269113




            34.9k1269113










            answered Feb 27 at 19:10









            Brian Olson

            1




            1







            • 1




              see this: ip addr | awk ' !/127.0.0.1/ && /inet/ gsub(//.*/, "", $2); print "IP="$2 '
              – Rui F Ribeiro
              Feb 27 at 19:25






            • 1




              Why are you grepping for the localhost IP 127.0.0.1?
              – Jeff Schaller
              Feb 27 at 20:12












            • 1




              see this: ip addr | awk ' !/127.0.0.1/ && /inet/ gsub(//.*/, "", $2); print "IP="$2 '
              – Rui F Ribeiro
              Feb 27 at 19:25






            • 1




              Why are you grepping for the localhost IP 127.0.0.1?
              – Jeff Schaller
              Feb 27 at 20:12







            1




            1




            see this: ip addr | awk ' !/127.0.0.1/ && /inet/ gsub(//.*/, "", $2); print "IP="$2 '
            – Rui F Ribeiro
            Feb 27 at 19:25




            see this: ip addr | awk ' !/127.0.0.1/ && /inet/ gsub(//.*/, "", $2); print "IP="$2 '
            – Rui F Ribeiro
            Feb 27 at 19:25




            1




            1




            Why are you grepping for the localhost IP 127.0.0.1?
            – Jeff Schaller
            Feb 27 at 20:12




            Why are you grepping for the localhost IP 127.0.0.1?
            – Jeff Schaller
            Feb 27 at 20:12












             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f426883%2fhow-to-capture-the-first-ip-address-from-a-ifconfig-command%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)