一般来说,研究 ifconfig.c 源代码就可以达到目的了。
但是Linux已经提供了比较方便的获取网口信息的方式:
[philip@catonbj ~]$ cat /sys/class/net/em1/statistics/rx_bytes
3911191274
在/sys/class/net/INTERFACE/statistics/ 目录下有所有网口的状态:
[philip@catonbj statistics]$ ls
collisions rx_bytes rx_crc_errors rx_errors rx_frame_errors rx_missed_errors rx_packets tx_bytes tx_compressed tx_errors tx_heartbeat_errors tx_window_errors
multicast rx_compressed rx_dropped rx_fifo_errors rx_length_errors rx_over_errors tx_aborted_errors tx_carrier_errors tx_dropped tx_fifo_errors tx_packets
而它的上一级:
[philip@catonbj em1]$ ls
addr_assign_type address addr_len broadcast carrier device dev_id dormant duplex flags ifalias ifindex iflink link_mode mtu netdev_group operstate power queues speed statistics subsystem tx_queue_len type uevent
也有较详细的描述。
程序上或许获取接口比较难用,何不直接调用 cat 句柄调用呢?