Get MAC address using POSIX APIs
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#define MAXINTERFACES 16
void get_ip()
{
int sock_fd;
struct ifreq buf[MAXINTERFACES];
struct ifconf ifc;
int interface_num;
char *addr;//[ADDR_LEN];
if((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("Create socket failed");
return;
}
ifc.ifc_len = sizeof(buf);
ifc.ifc_req = buf;
if(ioctl(sock_fd, SIOCGIFCONF, (char *)&ifc) < 0)
{
printf("Get a list of interface addresses failed");
return;
}
interface_num = ifc.ifc_len / sizeof(struct ifreq);
printf("The number of interfaces is %d\n", interface_num);
while(interface_num--)
{
printf("Net device: %s\n", buf[interface_num].ifr_name);
if(ioctl(sock_fd, SIOCGIFFLAGS, (char *)&buf[interface_num]) < 0)
{
printf("Get the active flag word of the device");
continue;
}
if(buf[interface_num].ifr_flags & IFF_PROMISC)
printf("Interface is in promiscuous mode\n");
if(buf[interface_num].ifr_flags & IFF_UP)
printf("Interface is running\n");
else
printf("Interface is not running\n");
if(ioctl(sock_fd, SIOCGIFADDR, (char *)&buf[interface_num]) < 0)
{
printf("Get interface address failed");
continue;
}
addr = inet_ntoa(((struct sockaddr_in*)(&buf[interface_num].ifr_addr))->sin_addr);
printf("IP address is %s\n", addr);
}
close(sock_fd);
return;
}
void get_mac()
{
int sock_fd;
struct ifreq buf[MAXINTERFACES];
struct ifconf ifc;
int interface_num;
if((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("Create socket failed");
return;
}
ifc.ifc_len = sizeof(buf);
ifc.ifc_req = buf;
if(ioctl(sock_fd, SIOCGIFCONF, (char *)&ifc) < 0)
{
printf("Get a list of interface addresses failed");
return;
}
interface_num = ifc.ifc_len / sizeof(struct ifreq);
printf("The number of interfaces is %d\n", interface_num);
while(interface_num--)
{
printf("Net device: %s\n", buf[interface_num].ifr_name);
if(ioctl(sock_fd, SIOCGIFFLAGS, (char *)&buf[interface_num]) < 0)
{
printf("Get the active flag word of the device");
continue;
}
if(buf[interface_num].ifr_flags & IFF_PROMISC)
printf("Interface is in promiscuous mode\n");
if(buf[interface_num].ifr_flags & IFF_UP)
printf("Interface is running\n");
else
printf("Interface is not running\n");
if(ioctl(sock_fd, SIOCGIFHWADDR, (char *)&buf[interface_num]) < 0)
{
printf("Get the hardware address of a device failed");
continue;
}
printf("Mac address is: %02X:%02X:%02X:%02X:%02X:%02X\n",
(unsigned char)buf[interface_num].ifr_hwaddr.sa_data[0],
(unsigned char)buf[interface_num].ifr_hwaddr.sa_data[1],
(unsigned char)buf[interface_num].ifr_hwaddr.sa_data[2],
(unsigned char)buf[interface_num].ifr_hwaddr.sa_data[3],
(unsigned char)buf[interface_num].ifr_hwaddr.sa_data[4],
(unsigned char)buf[interface_num].ifr_hwaddr.sa_data[5]);
}
close(sock_fd);
}
Get MAC address using POSIX APIs的更多相关文章
- Ubuntu 下,修改 Mac address
ifconfig // check Mac address sudo ifconfig eth0 down sudo ifconfig eth0 hw ether xx:xx:xx:xx: ...
- 【小错误】Device eth2 has different MAC address than expected, ignoring.
今天在搭建rac配置IP的时候报错显示如下: Device eth2 has different MAC address than expected, ignoring.[FAILED] 百度了下,问 ...
- Find mac address
Windows Method 1: Using the Command Prompt 1 Click on the Start button. 2 Type cmd in the search b ...
- OK335xS mac address hacking
/*********************************************************************** * OK335xS mac address hacki ...
- 利用C语言获取设备的MAC address
利用C语言获取设备的MAC address MAC address --> Medium Access Control layer address // // http://www.binary ...
- 关于获得本机Mac Address的方法
网络上有讲获得Mac address的方法有如下: 1. 发送ARP命令,利用返回的Mac Address缓冲区得到 2. 用NetworkInterface.GetAllNetworkInterfa ...
- what is MAC address
MAC Address:media access control address A media access control address (MAC address) is a unique id ...
- vb.net 使用ip查詢(Host Name)(WorkGroup Name)(MAC Address)-運用cmd及nbtstat命令
Sub nbtstat(ByVal ip As String) Dim strRst, strRst1, strRst2, strRst3 As String Dim n1, n2, n3 As In ...
- How to Change MAC Address on Ubuntu
1 Open Terminal. 2 Log in as root so type: sudo -i and then write your password. 3 View your cur ...
随机推荐
- shell编程——日志输出的同时显屏
在执行脚本的时候我们常常需要将执行过程全部输出到日志里,以备出现报错时可以跟踪分析,开始我用的是exec: exec 1>info.log #把全部执行过程输出到info日志中 exec 2&g ...
- Array.prototype.slice用法详解
slice方法是定义在js数组原型中的方法,用于截取数组的部分元素,具体使用如下: arrayExample.slice(start, end); start为起始元素位置,end为截止元素位置,如: ...
- WWW.LoadFromCacheOrDownload
[WWW.LoadFromCacheOrDownload] static WWWLoadFromCacheOrDownload(string url, int version, uint crc = ...
- Eclipse创建javaWeb项目工程
首先,想作为一个较为标准的程序员,应该把自己的开发工具eclipse设置成与公司中大多数程序员的设置一样,比如说工作的字符编码为UTF-8,字体大小等等. 在刚下载好的eclipse中刚进去是没有To ...
- XIb中使用tableview报错UIViewAlertForUnsatisfiableConstraints
1.使用断点工具并不能找出错误,最后仔细看了下报错信息 2.报错信息 [LayoutConstraints] Unable to simultaneously satisfy constraints. ...
- Intellij IDEA 安装插件 报 ‘plugin xxxx is incompatible‘ 解决方案
网上下载安装LOMBOK失败,直接下载插件安装: 在离线安装IDEA插件的时候,可能会出现该问题.引起的原因主要就是版本号不一致. 下面介绍下离线安装找到合适的版本号. 1.在IDEA的help-&g ...
- 4418开发板读取u盘说明
1.插上u盘后会在dev下生成两个文件db db1 将db1挂载即可访问..
- $.ajax()函数
一般在前端html和服务器交互,又要异步提交表单时,我们通常会用到$.ajax(){}函数,这是封装到ajax里的一个函数,相比于XMLHTTPRequest做页面局部刷新更方便,但最终还是使用的XM ...
- java简单例子介绍IOC和AOP
IOC和AOP的一些基本概念 介绍 IOC 一.什么是IOC IoC就是Inversion of Control,控制反转.在Java开发中,IoC意味着将你设计好的类交给系统去控制,而不是在你的类内 ...
- ajax 整个表单的提交
重点:data: $("#form1").serialize() function setSaveNext() { setSaveData(); var cx = pageInde ...