关闭

Linux下获取所有可用网卡信息

标签: linux网卡mac
351人阅读 评论(1) 收藏 举报
分类:

Linux下开发网络程序时,经常会遇到需要取本地网络接口名、IP广播地址、子网掩码或者MAC地址等信息的需求,最常见的办法是配合宏SIOCGIFHWADDR、SIOCGIFADDR、SIOCGIFBRDADDR与SIOCGIFNETMASK作为参数调用函数ioctl分别获得MAC地址、IP地址、广播地址与子网掩码来实现。一次性获取此类信息的C语言代码实现如下。  

#include <stdio.h>
#include <string.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <errno.h>

int getLocalInfo(void)
{
int fd;
int interfaceNum = 0;
struct ifreq buf[16];
struct ifconf ifc;
struct ifreq ifrcopy;
char mac[16] = {0};
char ip[32] = {0};
char broadAddr[32] = {0};
char subnetMask[32] = {0};

if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("socket");
close(fd);
return -1;
}

ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t)buf;
if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc))
{
interfaceNum = ifc.ifc_len / sizeof(struct ifreq);
printf("interface num = %d\n", interfaceNum);
while (interfaceNum-- > 0)
{
printf("\ndevice name: %s\n", buf[interfaceNum].ifr_name);

//ignore the interface that not up or not runing
ifrcopy = buf[interfaceNum];
if (ioctl(fd, SIOCGIFFLAGS, &ifrcopy))
{
printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -1;
}

//get the mac of this interface
if (!ioctl(fd, SIOCGIFHWADDR, (char *)(&buf[interfaceNum])))
{
memset(mac, 0, sizeof(mac));
snprintf(mac, sizeof(mac), "%02x%02x%02x%02x%02x%02x",
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[0],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[1],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[2],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[3],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[4],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[5]);
printf("device mac: %s\n", mac);
}
else
{
printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -1;
}

//get the IP of this interface
if (!ioctl(fd, SIOCGIFADDR, (char *)&buf[interfaceNum]))
{
snprintf(ip, sizeof(ip), "%s",
(char *)inet_ntoa(((struct sockaddr_in *)&(buf[interfaceNum].ifr_addr))->sin_addr));
printf("device ip: %s\n", ip);
}
else
{
printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -1;
}

//get the broad address of this interface
if (!ioctl(fd, SIOCGIFBRDADDR, &buf[interfaceNum]))
{
snprintf(broadAddr, sizeof(broadAddr), "%s",
(char *)inet_ntoa(((struct sockaddr_in *)&(buf[interfaceNum].ifr_broadaddr))->sin_addr));
printf("device broadAddr: %s\n", broadAddr);
}
else
{
printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -1;
}

//get the subnet mask of this interface
if (!ioctl(fd, SIOCGIFNETMASK, &buf[interfaceNum]))
{
snprintf(subnetMask, sizeof(subnetMask), "%s",
(char *)inet_ntoa(((struct sockaddr_in *)&(buf[interfaceNum].ifr_netmask))->sin_addr));
printf("device subnetMask: %s\n", subnetMask);
}
else
{
printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -1;
}
}
}
else
{
printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -1;
}

close(fd);
return 0;
}

int main(void)
{
getLocalInfo();
return 0;
}

保存为getmac.c,编译make getmac,执行getmac的结果

interface num = 2

device name: eth0
device mac: 000a227da213
device ip: 192.168.2.19
device broadAddr: 192.1168.255.255
device subnetMask: 255.255.0.0

device name: lo
device mac: 000000000000
device ip: 127.0.0.1
device broadAddr: 0.0.0.0
device subnetMask: 255.0.0.0

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:220797次
    • 积分:3179
    • 等级:
    • 排名:第10824名
    • 原创:130篇
    • 转载:7篇
    • 译文:0篇
    • 评论:31条
    最新评论