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 ...
随机推荐
- 打开当前目录的其他exe
STARTUPINFO si; PROCESS_INFORMATION pi; memset(&si, , sizeof(si)); si.cb = sizeof(STARTUPINFO); ...
- C#基础编程试题 4
一.题目 使用递归算法和二维数组,根据下面条件排序上面的对象. 五个人 张三 李四 王五 猴六 麻七 五间房子 黄房子 蓝房子 红房子 绿房子 橙房子 五只宠物 蜗牛 小狗 小猫 小白兔 小金鱼 五个 ...
- objective-C中的"非正式协议"和“正式协议”
objective-C中的接口与泛型 先承认我是标题党,因为在obj-c的世界中,官方根本没有"接口"与"泛型"这样的说法. 不过在obj-c中有二个与之接近的 ...
- Python OrderedDict使用
一.最近最少使用实现: import collections class LRUDict(object): ''' 最近最少使用队列实现,最近使用的键值放后面 ''' def __init__(sel ...
- java 蓝桥杯算法提高 _1区间k大数查询
import java.util.Scanner; public class _1区间K大数查询 { public static void main(String[] args) { Scanner ...
- bash's parameter expansion
[bash's parameter expansion] #: find first from left, remove-left ##: find last from left, remove le ...
- DBArtist之Oracle入门第2步: 了解Oracle的Database Control
之前安装好数据库后,会有下面这个弹窗,然后根据Database Control URL地址进入瞧一瞧,看一看! 根据地址进入以后,是一个登录界面,用system账户登录,密码就是安装Oracle的时候 ...
- 解决table边框在打印中不显示的问题
先了解一下,table边框如何设置 一.只对表格table标签设置边框 只对table标签设置border(边框)样式,将让此表格最外层table一个边框,而表格内部不产生边框样式.CSS代码: .t ...
- sqlserver服务器硬件性能瓶颈分析
硬件性能瓶颈 内存 内存对SQL Server性能的影响胜过任何其他硬件.因此,对SQL Server系统的内存使用情况进行定期监视以确保内存的可用百分比高于20%是很有必要的.如果用户遭遇性能问题, ...
- Zedboard学习(一):移植Ubuntu桌面操作系统 标签: ubuntu移植zedboardFPGA 2017-07-04 21:53 26人阅读
环境准备: 首先,需要的肯定是Ubuntu操作系统.可以在自己的电脑上安装物理机,也可以是虚拟机下运行的.我的是在Vmware下运行的Ubuntu14.04 32位操作系统. 由于zedboard上的 ...