利用C语言获取设备的MAC address


MAC address --> Medium Access Control layer address

//
// http://www.binarytides.com/c-program-to-get-mac-address-from-interface-name-on-linux/ #include <stdio.h> //printf
#include <string.h> //strncpy
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h> //ifreq
#include <unistd.h> //close void getmacaddr(char *macaddr, const char *interface)
{
int fd;
struct ifreq ifr;
const char *iface = interface;
unsigned char *mac; fd = socket(AF_INET, SOCK_DGRAM, 0); ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name , iface , IFNAMSIZ-1); ioctl(fd, SIOCGIFHWADDR, &ifr); close(fd); mac = (unsigned char *)ifr.ifr_hwaddr.sa_data; sprintf(macaddr, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" , mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); } int main()
{
char macaddress[17]={'0'};
getmacaddr(macaddress, "eth0"); int i=0;
for(i=0;i<sizeof(macaddress);i++)
{
printf("%c", macaddress[i]);
}
printf("\n"); return 0;
}

这里有一个需要玩味的是,函数getmacaddr()同样可以使用二维字符之称char **作为参数,但是需要注意的是,不能直接拿参数来操作,需要设置临时变量。

void _getmacaddr(char **macaddr, const char *interface)
{
int fd;
struct ifreq ifr;
const char *iface = interface;
unsigned char *mac;
char **newaddress = macaddr; fd = socket(AF_INET, SOCK_DGRAM, 0); ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name , iface , IFNAMSIZ-1); ioctl(fd, SIOCGIFHWADDR, &ifr); close(fd); mac = (unsigned char *)ifr.ifr_hwaddr.sa_data; sprintf( *newaddress, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" , mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); }

参考:


1、Get MAC Address

2、How to get mac address

利用C语言获取设备的MAC address的更多相关文章

  1. 关于获得本机Mac Address的方法

    网络上有讲获得Mac address的方法有如下: 1. 发送ARP命令,利用返回的Mac Address缓冲区得到 2. 用NetworkInterface.GetAllNetworkInterfa ...

  2. ios7 以后准确获取iphone设备的MAC(物理地址)

    通过参考 钉钉 项目,知道是通过wifi拿到路由的MAC地址.那么可不可以拿到iphone 设备的MAC 地址呢? 经过一番搜索,发现所有文章都是针对 ios 7 以前 可以拿到. 而且方法也都是同一 ...

  3. ARP-NAT(MAC Address Translation)的原理

    本文部分图片来自: http://wiki.deliberant.com/faq/wireless-bridge-routing-arpnat/ https://wiki.openwrt.org/do ...

  4. Ubuntu 下,修改 Mac address

    ifconfig    //    check Mac address sudo ifconfig eth0 down sudo ifconfig eth0 hw ether xx:xx:xx:xx: ...

  5. 【小错误】Device eth2 has different MAC address than expected, ignoring.

    今天在搭建rac配置IP的时候报错显示如下: Device eth2 has different MAC address than expected, ignoring.[FAILED] 百度了下,问 ...

  6. 获取设备的mac地址可靠的方法

    参考自:http://www.open-open.com/lib/view/open1433406847322.html /** * 获取设备的mac地址 * * @param ac * @param ...

  7. Find mac address

    Windows Method 1: Using the Command Prompt 1 Click on the Start button.   2 Type cmd in the search b ...

  8. OK335xS mac address hacking

    /*********************************************************************** * OK335xS mac address hacki ...

  9. what is MAC address

    MAC Address:media access control address A media access control address (MAC address) is a unique id ...

随机推荐

  1. 如何创建一个有System用户权限的命令行

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何创建一个有System用户权限的命令行.

  2. Android设计模式系列--工厂方法模式

    工厂方法模式,往往是设计模式初学者入门的模式,的确,有人称之为最为典型最具启发效果的模式.android中用到了太多的工厂类,其中有用工厂方法模式的,当然也有很多工厂并不是使用工厂方法模式的,只是工具 ...

  3. Mysql 培训

     1. Mysql 培训 1.1. 培训目的 本文档是针对MySQL 数据库方面的基础培训,为了使项目组成员能够达到使用MySQL 数据库的目的. 1.2. 培训对象 开发者 1.3. 经常使用词及符 ...

  4. 文件和目录之stat、fstat和lstat函数

    #include <sys/stat.h> int stat( const char *restrict pathname, struct stat *restrict buf ); in ...

  5. debian 系统备份

    tar -zcvpf /home/full-backup.tar.gz / --exclude=/mnt/* --exclude=/proc/* --exclude=/sys/* 这个命令是把根目录下 ...

  6. C#_IComparer实例 - 实现ID或者yearOfscv排序

    调用LIST的Sort的时候会调用IComparer的默认实现,quicksort会调用每个元素的CompareTo的IComparable实现 using System; using System. ...

  7. Windows7中Emacs 24 shell使用Gitbash

    今天发现可以在shell中直接打开Gitbash,Gitbash提供了一些有用的Linux风格命令,最关键是我用emacs的时候不用再打开一个Gitbash终端操纵Git了. 在~/.emacs.d/ ...

  8. Database ORM

    Database ORM Introduction Basic Usage Mass Assignment Insert, Update, Delete Soft Deleting Timestamp ...

  9. C# 之 OpenFileDialog的使用

    一.打开文件对话框(OpenFileDialog) 1. OpenFileDialog控件有以下基本属性 [1]InitialDirectory:对话框的初始目录 [2]Filter:要在对话框中显示 ...

  10. html5 list属性、autocomplete属性、pattern属性

    list属性的值为某个datalist元素的id,datalist也是html5中新增的属性,类似于选择框select,或者android中的autocomplete textview控件.datal ...