How can I programmatically find the IP address/netmask/gateway configured for a specific network dev
Posted
by Oren S
on Stack Overflow
See other posts from Stack Overflow
or by Oren S
Published on 2010-06-07T12:27:33Z
Indexed on
2010/06/07
12:52 UTC
Read the original article
Hit count: 326
Hi
I would like to write a piece of code which checks, for each network device (e.g. eth0, lo, master devices) some statistics and configuration data about that device.
I could find the statistics data (and most of the configuration data) in /sys/class/net/..., however, I couldn't find any C/C++ API or any entry in procfs/sysfs listing the inet addr, netmask and gateway.
Some alternatives I checked:
- parsing the output from ifconfig/route/some other utilities: I don't want to start a subprocess every time I need to do the inspection.
- parsing /etc/sysconfig/network-scripts/: will give me only the start-up configuration, and not the current state.
Also, since this code is intended for a product in my workplace, where every external library is inspected thoroughly (meaning it will take me forever to add any external library) I prefer solutions which rely on Linux native API and not external libraries.
Thanks!
© Stack Overflow or respective owner