开发者

Linux/C: Get ip address from device name? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Get IP address of an interface on linux

How can I get the 开发者_如何学编程ip address from the device name (Example: eth0)?


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.


ip addr

or

ip addr show eth0

or the obsolete

ifconfig eth0

And this is a question or serverfault.com


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜