Search code examples
linuxifconfig

How do I get the IP of only one Interface


When I tried ifconfig it gives me the whole all the information regarding the Network Adapter.

I tried :

system( "ifconfig -a | grep inet | "
          "sed 's/\\([ ]*[^ ]*\\)\\([ ]*[^ ]*\\).*$/\\1 \\2/' "
          " > address.txt" ) ;

which output two Ips :

  inet  addr:17.24.17.229
  inet  addr:127.0.0.1

But I need just the 1st one , How can I filter this out.


Solution

  • Don't look at all of the adapters, just the one you want.

    system( "ifconfig -a eth0 | grep inet | "
              "sed 's/\\([ ]*[^ ]*\\)\\([ ]*[^ ]*\\).*$/\\1 \\2/' "
              " > address.txt" ) ;