ioctrl 获取本机IP及MAC地址
通过使用ioctl可以获得本机的一些信息,这里记录获得interface IP及MAC的过程。
1:ioctl 函数的作用是什么
man ioctl:
DESCRIPTION
The ioctl() function manipulates the underlying device parameters of special files. In particular, many operating characterisâ€
tics of character special files (e.g., terminals) may be controlled with ioctl() requests. The argument fd must be an open file
descriptor.
The second argument is a device-dependent request code. The third argument is an untyped pointer to memory. It's traditionally
char *argp (from the days before void * was valid C), and will be so named for this discussion.
An ioctl() request has encoded in it whether the argument is an in parameter or out parameter, and the size of the argument argp
in bytes. Macros and defines used in specifying an ioctl() request are located in the file <sys/ioctl.h>.
2:测试例子
/*
用socket获得当前interface的信息
1:ip地址
2:mac地址
3:interface index
注:创建的是原始套接字,编译及执行都需要root权限
*/
int read_interface(const char* interface)
{
int fd;
int ret = ;
struct ifreq ifr;
struct sockaddr_in *ip_addr = NULL;
char mac[] = {};
memset(&ifr, , sizeof(struct ifreq));
fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if (fd > ){
ifr.ifr_addr.sa_family = AF_INET;
strcpy(ifr.ifr_name, interface); //get interface ip
if(ioctl(fd, SIOCGIFADDR, &ifr) == ){
ip_addr = (struct sockaddr_in *)&ifr.ifr_addr;
printf("The %s ip addr is %s\n",ifr.ifr_name,inet_ntoa(ip_addr->sin_addr));
} else {
printf("SIOCGIFADDR failed, error:%s\n", strerror(errno));
ret = -;
goto out;
} //get interface mac
if(ioctl(fd, SIOCGIFHWADDR, &ifr) == ){
memcpy(mac, ifr.ifr_hwaddr.sa_data, );
printf("interface mac: %02X:%02X:%02X:%02X:%02X:%02X\n",
mac[],mac[],mac[],mac[],mac[],mac[]);
} else {
printf("SIOCGIFHWADDR failed, error:%s\n", strerror(errno));
ret = -;
goto out;
} //get interface index
if (ioctl(fd, SIOCGIFINDEX, &ifr) == ) {
printf("The interface index:%d\n",ifr.ifr_ifindex);
} else {
printf("SIOCGIFINDEX failed, error:%s\n", strerror(errno));
ret = -;
goto out;
}
} else {
printf("%s\n",strerror(errno));
ret = -;
}
out:
close(fd);
return ret;
}
3:ioctl的第二个参数有很多,了解它们可以在需要时使用
参考:http://blog.csdn.net/evenness/article/details/7665970
ioctrl 获取本机IP及MAC地址的更多相关文章
- 获取本机IP、mac地址、计算机名
python获取本机IP.mac地址.计算机名 在python中获取ip地址和在php中有很大不同,我们先来看一下python 获得本机MAC地址: >>> import uuid ...
- python获取本机IP、mac地址、计算机名
在python中获取ip地址和在php中有很大不同,在php中往往比较简单.那再python中怎么做呢? 我们先来看一下python 获得本机MAC地址: 1 2 3 4 import uuid de ...
- Linux 获取本机IP、MAC地址用法大全
getifaddrs()和struct ifaddrs的使用,获取本机IP ifaddrs结构体定义如下: struct ifaddrs { struct ifaddrs *ifa_next; /* ...
- JAVA获取本机IP和Mac地址
在项目中,时常需要获取本机的Ip或是Mac地址,进行身份和权限验证,本文就是通过java代码获取ip和Mac. package com.svse.query;import java.net.In ...
- QT获取本机IP和Mac地址
#include <QNetworkInterface> #include <QList> void MainWindow::getIPPath() { QString str ...
- java 获取本机ip及mac地址
package com.achun.test; import java.net.Inet4Address;import java.net.Inet6Address;import java.net.In ...
- c#中如何获取本机用户名、MAC地址、IP地址、硬盘ID、CPU序列号、系统名称、物理内存
我们在利用C#开发桌面程序(Winform)程序的时候, 经常需要获取一些跟系统相关的信息, 以下这些代码获取能有些用处. c#中如何获取本机用户名.MAC地址.IP地址.硬盘ID.CPU序列号.系统 ...
- win32 获取本机网卡信息(MAC地址,IP地址等)
由于一个需求需要获取网卡的MAC地址,就搜了一下,大部分都是COPY来COPY去的一些代码,有很多甚至不能直接运行或有还有内存泄漏.自己查了一下MSDN然后封装了一下: 需要注意,一个机器可能有多个网 ...
- node.js获取本机Ip, hostName, mac
//获取ip地址 getIPAdress() { let interfaces = require('os').networkInterfaces(); for (var devName in int ...
随机推荐
- 166. Nth to Last Node in List
Description Find the nth to last element of a singly linked list. The minimum number of nodes in lis ...
- 安迪的第一个字典 (Andy's First Dictionary,UVa10815)
题目描述: #include<iostream> #include<string> #include<set> #include<sstream> us ...
- spring boot 中文乱码问题
在刚接触spring boot 2.0的时候,遇到了一些中文乱码的问题,网上找了一些解决方法. 这里自己做个汇总. 在application.properties文件中添加: spring.http. ...
- Tengine/Nginx 安装
原文出处:http://my.oschina.net/liuhuan0927/blog/604663 一.Tengine是什么 简介 Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基 ...
- php 连接redis查询数据
class Layoutdemo{ function index(){ $db = new Db(); $id=390; $layout_json = array(); if($info = $db- ...
- protected、public、private
一.protected成员 1. 受保护的成员的可访问性 对于一个类的protected成员,①该类的用户(如类对象)不能访问它,②该类的派生类的成员(及其友元)可以访问它. 派生类的成员及其友元不能 ...
- lintcode-34-N皇后问题 II
34-N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 标签 递归 思路 参考http://www.cnblogs.com ...
- TCP系列23—重传—13、RACK重传
一.RACK概述 RACK(Recent ACKnowledgment)是一种新的基于时间的丢包探测算法,RACK的目的是取代传统的基于dupthresh门限的各种快速重传及其变种.前面介绍的各种基于 ...
- 3dContactPointAnnotationTool开发日志(二一)
今天完成了修改按钮颜色,添加smpl模型到工具,以及可以显示物体子物体对应选项卡的功能.把之前的meshRenderer+meshFilter都改成了skinnedMeshRenderer,因为s ...
- 【log4net】- 非常完善的Log4net详细说明
1.概述 log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的媒介.本文主要是介绍如何在Visual S ...