日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Linux 下获取本机所有网卡 以及 网卡对应ip 列表

發布時間:2023/11/27 生活经验 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux 下获取本机所有网卡 以及 网卡对应ip 列表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡單record 一下

#include <arpa/inet.h>  // 'struct sockaddr_in'
#include <errno.h>
#include <net/if.h>  // 'struct ifreq' and 'struct if_nameindex'
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>  // ioctl
#include <unistd.h>int GetInterfaceIp(const char *eth_inf, char *ip) {int sd;struct sockaddr_in sin;struct ifreq ifr;sd = socket(AF_INET, SOCK_DGRAM, 0);if (-1 == sd) {printf("socket error: %s\n", strerror(errno));return -1;}strncpy(ifr.ifr_name, eth_inf, IFNAMSIZ);ifr.ifr_name[IFNAMSIZ - 1] = 0;// if error: No such deviceif (ioctl(sd, SIOCGIFADDR, &ifr) < 0) {printf("iterfac %s ioctl error: %s\n", eth_inf, strerror(errno));close(sd);return -1;}printf("interfac: %s, ip: %s\n", eth_inf,inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));close(sd);return 0;
}void GetAllInterfaceNames() {struct if_nameindex *if_nidxs, *intf;// the begin of if_name index.if_nidxs = if_nameindex();if (if_nidxs != NULL) {for (intf = if_nidxs; intf->if_index != 0 || intf->if_name != NULL;intf++) {char ip[1024];GetInterfaceIp(intf->if_name, ip);}if_freenameindex(if_nidxs);}
}int main() {GetAllInterfaceNames();return 0;
}

編譯及運行:

$ g++ -O0 -g get_ip.c -o get_ip
$ ./get_ip
interfac: lo, ip: 127.0.0.1
iterfac eth01 ioctl error: Cannot assign requested address
iterfac eth02 ioctl error: Cannot assign requested address
interfac: bond0, ip: 10.192.25.173

總結

以上是生活随笔為你收集整理的Linux 下获取本机所有网卡 以及 网卡对应ip 列表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。