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 ...
随机推荐
- Alternative PHP Cache ( APC )
简介: Alternative PHP Cache (APC) 是一个开放自由的PHP opcode 缓存.它的目标是提供一个自由.开放和健全的框架用于缓存和优化 PHP 的中间代码,加快 PHP 执 ...
- Core 第三组 结对作业——四则运算 Part1. Core代码编写
结对作业——四则运算 Part1. Core代码编写 PB15061303 刘梓轩PB16061489 艾寅中 GITHUB 地址 戳这里 目录 (因为内容较多,分为了三个部分,但作业系统中只能提交一 ...
- Linux实战教学笔记34:企业级监控Nagios实践(上)
一,Nagios监控简介 生活中大家应该对监控已司空见惯了,例如:餐馆门前的监控探头,小区里的视频监控,城市道路告诉监控探头等,这些监控的目的大家都很清楚,无须多说.那么,企业工作中为什么要部署监控系 ...
- Stencil
[Stencil] The stencil buffer can be used as a general purpose per pixel mask for saving or discardin ...
- 解剖Nginx·模块开发篇(2)ngx_http_hello_world_module 模块基本结构定义
elloWorld 是一个典型的 location 模块.什么是 location 模块?在 Nginx 中,根据作用域,有 main 模块.server 模块.location 模块. 1 模块定义 ...
- redis 常用方法整理
1.进入redis redis-cli -p -h 192.168.0.100 -a q9pCeAEMAWEL 2.查询keys keys activity_mobile_* 3.赋值.查值.删除 s ...
- 使用Jenkins集成和自动化打包资料
1.手把手教你利用Jenkins持续集成iOS项目 http://www.jianshu.com/p/41ecb06ae95f 2.Jenkins+ Xcode+ 蒲公英 实现IOS自动化打包和分发 ...
- ATM取款机的数据库模拟开发和实战总结
一.ATM实战开发的简介. 学习了几天的Oracle,开始着手用数据库PL/SQL语言做一个简单的ATM取款机业务,主要是为了巩固数据库的知识,并非真正的去实现高端的业务.有兴趣的可以看看,希望对同胞 ...
- crontab学习笔记
一.crond与crontab简介 在Linux系统中,循环运行的例行性计划任务,是由 cron (crond) 这个系统服务来控制的,而crontab命令则被用来提交和管理用户的需要周期性执行的任务 ...
- Runnable和Thread实现多线程的区别(含代码)
转载请注明出处:http://blog.csdn.net/ns_code/article/details/17161237 Java中实现多线程有两种方法:继承Thread类.实现Runnable接口 ...