linux下获取mac地址
来源:互联网 发布:linux sleep cpu 编辑:程序博客网 时间:2024/05/16 08:38
在网上看到的程序,http://linux.die.net/man/7/netdevice
具体实现参考http://www.kegel.com/dkftpbench/dkftpbench-0.45/getifaddrs.c
char ifr_name[IFNAMSIZ]; /* Interface name */ union { struct sockaddr ifr_addr; struct sockaddr ifr_dstaddr; struct sockaddr ifr_broadaddr; struct sockaddr ifr_netmask; struct sockaddr ifr_hwaddr; short ifr_flags; int ifr_ifindex; int ifr_metric; int ifr_mtu; struct ifmap ifr_map; char ifr_slave[IFNAMSIZ]; char ifr_newname[IFNAMSIZ]; char *ifr_data; };};struct ifconf { int ifc_len; /* size of buffer */ union { char *ifc_buf; /* buffer address */ struct ifreq *ifc_req; /* array of structures */ };};===================================================================================================================================================
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <net/if.h>
int main(void)
{
char buf[8192] = {0};
struct ifconf ifc = {0};
struct ifreq *ifr = NULL;
int sck = 0;
int nInterfaces = 0;
int i = 0;
char ip[INET6_ADDRSTRLEN] = {0};
char macp[19];
struct ifreq *item;
struct sockaddr *addr;
/* Get a socket handle. */
sck = socket(PF_INET, SOCK_DGRAM, 0);
if(sck < 0)
{
perror("socket");
return 1;
}
/* Query available interfaces. */
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
if(ioctl(sck, SIOCGIFCONF, &ifc) < 0)
{
perror("ioctl(SIOCGIFCONF)");
return 1;
}
/* Iterate through the list of interfaces. */
ifr = ifc.ifc_req;
nInterfaces = ifc.ifc_len / sizeof(struct ifreq);
for(i = 0; i < nInterfaces; i++)
{
// item = &ifr[i];
item = &ifr[i];
addr = &(item->ifr_addr);
/* Get the IP address*/
if(ioctl(sck, SIOCGIFADDR, item) < 0)
{
perror("ioctl(OSIOCGIFADDR)");
}
if (inet_ntop(AF_INET, &(((struct sockaddr_in *)addr)->sin_addr), ip, sizeof ip) == NULL)
{
perror("inet_ntop");
continue;
}
/* Get the MAC address */
if(ioctl(sck, SIOCGIFHWADDR, item) < 0) {
perror("ioctl(SIOCGIFHWADDR)");
return 1;
}
/* display result */
sprintf(macp, " %02x:%02x:%02x:%02x:%02x:%02x",
(unsigned char)item->ifr_hwaddr.sa_data[0],
(unsigned char)item->ifr_hwaddr.sa_data[1],
(unsigned char)item->ifr_hwaddr.sa_data[2],
(unsigned char)item->ifr_hwaddr.sa_data[3],
(unsigned char)item->ifr_hwaddr.sa_data[4],
(unsigned char)item->ifr_hwaddr.sa_data[5]);
printf("%s\t%s\t%s\n", item->ifr_name, ip, macp);
}
return 0;
}
===========================================================================================================================================
- Linux下获取MAC地址
- linux下获取mac地址
- Linux下获取MAC地址
- 获取 Linux 下 MAC 地址
- linux下获取MAC地址,IP地址
- linux下获取网卡mac地址
- Linux下获取MAC地址(C语言版)
- Linux下如何获取MAC地址
- Linux下如何获取网络设备MAC地址
- LINUX下获取IP地址和MAC地址
- LINUX下获取IP地址和MAC地址
- LINUX下获取IP地址和MAC地址
- LINUX下获取IP地址和MAC地址
- linux下获取网卡IP地址、MAC地址、RX_BYTES、TX_BYTES
- linux获取mac地址
- Linux下获取CPUID、硬盘序列号与MAC地址
- 在linux下 获取本机MAC地址的函数
- linux下程序设计-获取本机MAC地址
- ubuntu 激活root用户
- linux协议栈超时重传机制
- Android结束Activity退出程序的一种有效方法
- java的锁机制
- Mysql中的触发器(库存、用户订单中用到)
- linux下获取mac地址
- gcc编译多个源文件
- iOS - UISearchBar自动补全words方法及禁用
- 自动补全(智能提示)原理与实现
- 内存数据库
- poj 2386 Lake Counting
- 利用Theme自定义Activity进入退出动画
- NSNotificationCenter用法总结
- poj 1182 食物链