使用 getifaddrs 获取 MAC 地址
有没有办法通过 getifaddrs() 获取接口的 MAC 地址?
我已经有了这个来获取 IP 地址,但我有点错过了 MAC
。我尝试在 getifaddrs()
中查找信息,但没有关于 MAC
地址的信息,
struct ifaddrs *iflist, *iface;
if (getifaddrs(&iflist) < 0)
{
perror("getifaddrs");
}
char addrp[INET6_ADDRSTRLEN];
char macp[INET6_ADDRSTRLEN];
int i=0;
for (iface = iflist; iface; iface = iface->ifa_next)
{
int af = iface->ifa_addr->sa_family;
const void *addr;
const void *mac;
switch (af)
{
case AF_INET:
addr = &((struct sockaddr_in *)iface->ifa_addr)->sin_addr;
break;
//get mac address somehow?
default:
addr = NULL;
}
if (addr)
{
if (inet_ntop(af, addr, addrp, sizeof addrp) == NULL)
{
perror("inet_ntop");
continue;
}
if (inet_ntop(af, mac, macp, sizeof macp) == NULL) // this is already for MAC add
{
perror("inet_ntop");
continue;
}
if (strcmp(addrp, "127.0.0.1") != 0)
{
strcat(tableO[i].IPaddr, addrp);
strcat(tableO[i].MACaddr, macp);
i++;
}
}
谢谢
Is there a way to get interface's MAC address via getifaddrs()
?
I already have this, to get IP addresses, but I have kinda missed MAC
. Ive tried to look for the information in getifaddrs()
, but there is nothing about MAC
addresses
struct ifaddrs *iflist, *iface;
if (getifaddrs(&iflist) < 0)
{
perror("getifaddrs");
}
char addrp[INET6_ADDRSTRLEN];
char macp[INET6_ADDRSTRLEN];
int i=0;
for (iface = iflist; iface; iface = iface->ifa_next)
{
int af = iface->ifa_addr->sa_family;
const void *addr;
const void *mac;
switch (af)
{
case AF_INET:
addr = &((struct sockaddr_in *)iface->ifa_addr)->sin_addr;
break;
//get mac address somehow?
default:
addr = NULL;
}
if (addr)
{
if (inet_ntop(af, addr, addrp, sizeof addrp) == NULL)
{
perror("inet_ntop");
continue;
}
if (inet_ntop(af, mac, macp, sizeof macp) == NULL) // this is already for MAC add
{
perror("inet_ntop");
continue;
}
if (strcmp(addrp, "127.0.0.1") != 0)
{
strcat(tableO[i].IPaddr, addrp);
strcat(tableO[i].MACaddr, macp);
i++;
}
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
getifaddrs() 已提供与每个接口关联的 MAC 地址。在 Linux 上,当您遇到 family == AF_PACKET 时,这就是 MAC 地址。 OSX / BSD 上也是如此,但在这种情况下,该系列将是 AF_LINK。
getifaddrs() already provides the MAC address associated with each interface. On Linux, when you bump into a family == AF_PACKET that is the MAC address. Same thing on OSX / BSD but in that case the family will be AF_LINK.
在 BSD 系统上,您可以直接使用 getifaddrs 来检索 MAC。
以下是完整的 macaddr.c 工具的示例:
On BSD systems you can use getifaddrs directly for retrieving MACs.
Here's an example of the complete macaddr.c tools:
在 Linux 上,您可以执行类似的操作
,但是您可能想要检查 struct sockaddr_ll 的更多成员,请参阅 此处 获取说明。
On Linux, you'd do something like this
Though, there might be more members of the struct sockaddr_ll you'd want to inspect, see here for a description.
结合几个答案,这适用于 Linux 和 Mac/iOS/BSD
Combining a couple answers, this works on both Linux and Mac/iOS/BSD
这是获取IP和MAC地址的代码
Here is the code for getting IP and MAC addresses
如果您使用的是 Unix,则需要查找
ioctl
SIOCGIFHWADDR
:请参阅
man netdevice
。If you're on Unix, you're looking for
ioctl
SIOCGIFHWADDR
:See
man netdevice
.我的实验采用不同的方法列出接口
There is my experiments with different approaches to list interfaces