struct ifreq 获取IP 和mac和修改mac
struct ifreq 获取IP 和mac和修改mac
配置ip地址和mask地址:
ifconfig eth0 192.168.50.22 netmask 255.255.255.0 up dns服务器有关的文件:
/etc/resolv.conf
修改网卡的mac地址的步骤:
方法1:
1.关闭网卡设备
ifconfig eth0 down
2.修改网卡mac地址:
ifconfig eth0 hw ether 00:0c:29:2b:45:9f
3.重启网卡设备:
ifconfig eth0 up
以上方法一修改后linux重启后MAC又恢复为原来的,为了下次启动时修改后的MAC仍有效,我们可以修改文件file: /etc/rc.d/rc.sysinit(RedFlag Linux为这个文件,其他版本的linux应该不同)的内容,在该文件末尾加以下内容:
ifconfig eth0 down ifconfig eth0 hw ether MAC地址 ifconfig eth0 up
方法2: 很简单的,只是在./etc/sysconfig/network-scripts/ifcfg-eth0中加入下面一句话:
MACADDR=00:AA:BB:CC:DD:EE
方法3:
Linux 下如何更改网卡MAC地址
--------------------------------------------------------------------------------
简单的办法是在/etc/rc.d/rc.sysinit文件中加入那些命令: ifconfig eth0 down ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx ifconfig eth0 up 因为这个脚本运行在network之前,所以,MAC跟IP就是对应的
方法4:
Linux下的MAC地址更改 首先用命令关闭网卡设备。 /sbin/ifconfig eth0 down 然后就可以修改MAC地址了。 /sbin/ifconfig eth0 hw ether xxxxxxxxxxx (其中xx是您要修改的地址) 最后重新启用网卡 /sbin/ifconfig eth0 up 网卡的MAC地址更改就完成了
struct ifreq { char ifr_name[IFNAMSIZ]; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; short ifru_flags; int ifru_metric; caddr_t ifru_data; } ifr_ifru; };
#include <net/if.h> #include <net/if_arp.h>
#include <arpa/inet.h> #include <errno.h>
#define ETH_NAME "eth0"
int main() { int sock; struct sockaddr_in sin; struct ifreq ifr; unsigned char arp[6] ;
sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock == -1) { perror("socket"); return -1; }
strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ); ifr.ifr_name[IFNAMSIZ - 1] = 0;
if (ioctl(sock, SIOCGIFADDR, &ifr) == 0) //获取ip {
memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
fprintf(stdout, "eth0: %s\n", inet_ntoa(sin.sin_addr)); }
if( ioctl( sock, SIOCGIFHWADDR, &ifr ) == 0 ) //获取mac
{
memcpy( arp, ifr.ifr_hwaddr.sa_data, 6 ); printf( "adapter hardware address %x:%x:%x:%x:%x:%x\n", arp[0], arp[1], arp[2], arp[3], arp[4], arp[5] );
} return 0; }
、、***********************************************************************************************
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string.h> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> #include <sys/ioctl.h> #include <net/if.h> #include <net/if_arp.h> #include <arpa/inet.h> #include <errno.h>
int ConfigMacAddress(unsigned char *pMac); int test();
int main() { unsigned char mac[6] = {0x00,0x0c,0xa2,0x29,0x9b,0x9c}; unsigned char *pMac = mac;
ConfigMacAddress(pMac); //test(); return 0; }
#ifdef ygt struct ifreq { char ifr_name[IFNAMSIZ]; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; short ifru_flags; int ifru_metric; caddr_t ifru_data; } ifr_ifru; };
struct ifreq { #define IFHWADDRLEN 6 #define IFNAMSIZ IF_NAMESIZE union { char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "en0". */ } ifr_ifrn;
union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short int ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[IFNAMSIZ]; /* Just fits the size */ char ifru_newname[IFNAMSIZ]; __caddr_t ifru_data; } ifr_ifru; }; #define ifr_name ifr_ifrn.ifrn_name /* interface name */ #define ifr_hwaddr ifr_ifru.ifru_hwaddr /* MAC address */ #define ifr_addr ifr_ifru.ifru_addr /* address */ #define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-p lnk */ #define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */ #define ifr_netmask ifr_ifru.ifru_netmask /* interface net mask */ #define ifr_flags ifr_ifru.ifru_flags /* flags */ #define ifr_metric ifr_ifru.ifru_ivalue /* metric */ #define ifr_mtu ifr_ifru.ifru_mtu /* mtu */ #define ifr_map ifr_ifru.ifru_map /* device map */ #define ifr_slave ifr_ifru.ifru_slave /* slave device */ #define ifr_data ifr_ifru.ifru_data /* for use by interface */ #define ifr_ifindex ifr_ifru.ifru_ivalue /* interface index */ #define ifr_bandwidth ifr_ifru.ifru_ivalue /* link bandwidth */ #define ifr_qlen ifr_ifru.ifru_ivalue /* queue length */ #define ifr_newname ifr_ifru.ifru_newname /* New name */ #define _IOT_ifreq _IOT(_IOTS(char),IFNAMSIZ,_IOTS(char),16,0,0) #define _IOT_ifreq_short _IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),1,0,0) #define _IOT_ifreq_int _IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),1,0,0)
#endif
#define ETH_NAME "eth0" int test() { int sock; struct sockaddr_in sin; struct ifreq ifr; unsigned char arp[6] ;
sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock == -1) { perror("socket"); return -1; }
strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ); ifr.ifr_name[IFNAMSIZ - 1] = 0;
if(ioctl(sock, SIOCGIFADDR, &ifr) == 0) //de dao ip { memcpy(&sin, &ifr.ifr_addr, sizeof(sin)); fprintf(stdout, "eth0: %s\n", inet_ntoa(sin.sin_addr)); } if( ioctl( sock, SIOCGIFHWADDR, &ifr ) == 0 ) //de dao mac { memcpy( arp, ifr.ifr_hwaddr.sa_data, 6 ); printf( "adapter hardware address %x:%x:%x:%x:%x:%x\n", arp[0], arp[1], arp[2], arp[3], arp[4], arp[5] ); } return 0; }
int ConfigMacAddress(unsigned char *pMac) { int m_socket = 0; struct ifreq mac; #if 1 m_socket = socket(AF_INET, SOCK_DGRAM, 0); if(m_socket > 0) { strcpy(mac.ifr_name, "eth0"); // "eth0" mac.ifr_hwaddr.sa_family = ARPHRD_ETHER; for(int i = 0; i < 6; i++) { mac.ifr_hwaddr.sa_data[i] = pMac[i]; }
if(ioctl(m_socket, SIOCSIFHWADDR, &mac) == 0) { fprintf(stderr, "config mac address successful!\n"); close(m_socket); return 0; } fprintf(stderr, "config mac address failed!\n"); close(m_socket); } #else char cmd[128] = {'\0'}; snprintf(cmd, 127, "ifconfig eth0 hw ether %x:%x:%x:%x:%x:%x", pMac[0], pMac[1], pMac[2], pMac[3], pMac[4], pMac[5]); fprintf(stderr, "cmd = %s\n", cmd); system(cmd); #endif return -1; }
struct ifreq 获取IP 和mac和修改mac的更多相关文章
- Mac 如何修改Mac系统的默认截图路径
step 1 :打在桌面或者其他任意位置创建一个文件夹:截图图库.我创建的路径是:/Users/yilin/Documents/截图图库(仅供参考) step 2:打开终端,输入以下命令:defaul ...
- struct ifreq学习和实例
一.struct ifreq结构体 这个结构定义在/usr/include/net/if.h,用来配置和获取ip地址,掩码,MTU等接口信息的. /* Interface request struct ...
- 修改MAC地址的方法 破解MAC地址绑定(抄)
修改MAC地址的方法 破解MAC地址绑定 网卡的MAC地址是固化在网上EPROM中的物理地址,是一块网卡的“身份证”,通常为48位.在平常的应用中,有很多方面与MAC地址相关,如有些软件是和MAC ...
- 获取网络接口信息——ioctl()函数与结构体struct ifreq、 struct ifconf
转载请注明出处:windeal专栏 Linux 下 可以使用ioctl()函数 以及 结构体 struct ifreq 结构体struct ifconf来获取网络接口的各种信息. ioctl 首先看 ...
- linux系统下获取IP,MAC,子网掩码,网关
获取IP和子网掩码 int getLocalInfo(char IP[],char Mask[]) { int fd; int interfaceNum = 0; struct ifreq buf[1 ...
- struct ifconf和struct ifreq,获取网线插入状态
这两天看用C获取当前网口的插入网线状态的程序,遇见了这两个不熟悉的结构体,看了头文件中的说明和详细. struct ifreq 这个结构定义在include/net/if.h,用来配置ip地址,激活接 ...
- struct ifreq结构体与ip,子网掩码,网关等信息
总结一下,今天学习的关于通过socket,ioctl来获得ip,netmask等信息,其中很多内容参照了很多网上的信息,我会一一列出的 我用的这个函数,就是下面这个函数,其中的有一些全局变量,很好懂, ...
- C语言实现Windows下获取IP和MAC地址。
C语言实现Windows下获取IP和MAC地址. #include <winsock2.h> #include <stdio.h> #include <stdlib.h& ...
- C# 获取本机CPU序列号,MAC地址,硬盘ID,本机IP地址,计算机名,物理内存,PC类型
首先引入服务 然后 调用 本文转载自http://blog.sina.com.cn/s/blog_7eeb43210101hf7f.html public class Computer { publi ...
随机推荐
- MyBatis_[tp_50]_动态sql_bind绑定 与原生sql对比
笔记要点出错分析与总结 更推荐,原生的sql写法,bind方法不灵活! Test中: e.setLastName("%e%"); 直接在这里写上模糊查询的语句,更加省时 配置中: ...
- eclipse等编辑器选中列快编辑的方法
一.eclipse 1.首先按alt+shift+a,开启块选择模式 2.使用鼠标进行块选择 3.再次按Alt+Shift+a即可关闭块选择模式 二.EditPlus 1.菜单:编辑 -> 选择 ...
- Nginx入门(三)——正向代理
server { resolver 114.114.114.114; #指定DNS服务器IP地址 listen 443; location / { proxy_pass https://$host$r ...
- 02 Spring IOC
我们先看看我们在没有spring之前,程序间是怎么解耦的.创建一个maven工程,整体的目录结构 1.创建dao层 IAccountDao.java package com.itzn.dao; pub ...
- PL/SQL 子查询
一.概述 在一个SQL语句中嵌套另一个SQL语句成为子查询.包括单行子查询,多行子查询,多列子查询. 注意,当在DDL语句中引用子查询时,可以带有Order By子句:但是当在where子句.Set子 ...
- Selenium常用API的使用java语言之6-WebDriver常用方法
前面我们已经学习了定位元素, 定位只是第一步, 定位之后需要对这个元素进行操作, 或单击(按钮) 或 输入(输入框) , 下面就来认识这些最常用的方法. 1.WebDriver 常用方法 下面先来认识 ...
- Codeforces Global Round 2 E. Pavel and Triangles(思维+DP)
题目链接:https://codeforces.com/contest/1119/problem/E 题意:有n种长度的棍子,有a_i根2^i长度的棍子,问最多可以组成多少个三角形 题解:dp[i]表 ...
- icpc 银川 H. Delivery Route SPFA优化
Problem Description Pony is the boss of a courier company. The company needs to deliver packages to ...
- 学到了林海峰,武沛齐讲的Day22-完 os sys json pickle shelve XML re
__ file__ ===== 文件路径 os.path.dirname( 路径 )=======到上一层目录 os sys
- zabbix数据的时序-
gj的proxy服务器经过重启之后时序有变化. zabbix数据库中数据的存储是以哪方为准server端还是agent端, 触发事件跟恢复时间反了,本应该恢复的事件在数据库中查询event,得到的事件 ...