2

Possible Duplicate:
Get IP address of an interface on linux

How can I get the ip address from the device name (Example: eth0)?

Community
  • 1
  • 1
Quintin
  • 109
  • 1
  • 7
  • Take a look at this question: [using C code to get same info as ifconfig](http://stackoverflow.com/questions/4951257/using-c-code-to-get-same-info-as-ifconfig/4951451#4951451) – Paul Rubel Sep 28 '11 at 22:15
  • pcap_lookupnet() will give you what you need for IPV4. Otherwise, I'd recommend just copying from the ifconfig source. – Art Taylor Sep 28 '11 at 22:18
  • 2
    It's already answered here: http://stackoverflow.com/questions/2283494/get-ip-address-of-an-interface-on-linux – Siavash Safi Sep 28 '11 at 22:23
  • Especially with IPv6 there can be more than one IP address per interface, therefore the question needs some revision. – Steve-o Sep 28 '11 at 22:27

3 Answers3

2

DISCLAIMER: Your application SHOULD NOT depend on this kind of information. The application must see and use IP addresses ONLY. Ethernet devices are operating system plumbing. Keep in mind that you may have IP addresses not associated with any device, or devices with multiple IP addresses, multiple protocols (IPv4, IPv6), etc. Recheck the design of your application if it is really expecting to use IP addresses associated to Ethernet device names.

If you still want to associate IP addresses and Ethernet device names, check getifaddrs(3), which is a simple frontend to netlink(7) kernel sockets.

Juliano
  • 39,173
  • 13
  • 67
  • 73
  • In some cases application it helps a great deal to specify device name and not IP, especially when app is moving from one server to another, where IPs are different, but intended NIC name is always the same. –  Sep 28 '11 at 22:17
  • 1
    `getifaddrs` good. Disclaimer wrong. Most applications should not use interface names. Some, which are designed to be intimate with the networking stack, absolutely should (go read [`man dnsmasq`](http://www.thekelleys.org.uk/dnsmasq/docs/dnsmasq-man.html)) – Ben Voigt Sep 28 '11 at 22:23
  • @VladLazarenko This is still the wrong solution to the problem. I guess you are using this to determine what address to bind() and listen(), right? In that case, you should either bind() to the wildcard address (INADDR_ANY), allow the user to configure the address manually, or use DNS to determine the address that should be bound. You are assuming that one eth device always has one and only one address, and that the user will never need to bind to an address not listed in any interface. Both assumptions are wrong. – Juliano Sep 28 '11 at 22:24
  • @BenVoigt dnsmask is not your average user application, and you know this; dnsmask is almost in the "operating system plumbing" I mentioned in my answer. Obviously, applications that service the network stack must see beyond the IP addresses. In my experience, when the average programmer tries to associate IP addresses to network devices, they have a design problem in their application. – Juliano Sep 28 '11 at 22:28
  • @VladLazarenko, I gave you three options, of course one of them, INADDR_ANY, will not be suitable for all cases. For those, the other two options should be used. You understand that? Please, keep your ad hominem arguments to yourself, and be careful about speculating the experience of people who you don't know. Also, your reasoning for downvoting my answer is dumb. You simply don't agree with one thing I said and can't provide justification... tsc. – Juliano Sep 29 '11 at 03:33
1
ip addr

or

ip addr show eth0

or the obsolete

ifconfig eth0

And this is a question or serverfault.com

rapadura
  • 5,242
  • 7
  • 39
  • 57
  • Since the author tagged it "c", this is actually adequate for stackoverflow.com and inadequate for serverfault.com (which is for systems and network administration questions). – Juliano Sep 28 '11 at 22:16
  • 1
    The requester asked about a c-based solution. A shell-based response will work, but he would have to pipe out to ifconfig etc. – Art Taylor Sep 28 '11 at 22:19
0

Look here. If you need to use your result in a C program, you can use system(yourCommand) and then fopen() stdout to read the result.

Andrew
  • 4,145
  • 2
  • 37
  • 42
  • 4
    This method is *not* cool! You expose your application to security vulnerabilities, race conditions (the time system executes) and potentially running the wrong command with the applications privileges in case someone puts the "yourCommand" on a higher priority in the path/environment than the system one you expect to run! See how the command ifconfig does it and do it like that, using system calls rather than executing system commands! – rapadura Sep 29 '11 at 16:57