2012-09-11 14:26

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的更多相关文章

  1. Mac 如何修改Mac系统的默认截图路径

    step 1 :打在桌面或者其他任意位置创建一个文件夹:截图图库.我创建的路径是:/Users/yilin/Documents/截图图库(仅供参考) step 2:打开终端,输入以下命令:defaul ...

  2. struct ifreq学习和实例

    一.struct ifreq结构体 这个结构定义在/usr/include/net/if.h,用来配置和获取ip地址,掩码,MTU等接口信息的. /* Interface request struct ...

  3. 修改MAC地址的方法 破解MAC地址绑定(抄)

    修改MAC地址的方法 破解MAC地址绑定   网卡的MAC地址是固化在网上EPROM中的物理地址,是一块网卡的“身份证”,通常为48位.在平常的应用中,有很多方面与MAC地址相关,如有些软件是和MAC ...

  4. 获取网络接口信息——ioctl()函数与结构体struct ifreq、 struct ifconf

    转载请注明出处:windeal专栏 Linux 下 可以使用ioctl()函数 以及 结构体 struct ifreq  结构体struct ifconf来获取网络接口的各种信息. ioctl 首先看 ...

  5. linux系统下获取IP,MAC,子网掩码,网关

    获取IP和子网掩码 int getLocalInfo(char IP[],char Mask[]) { int fd; int interfaceNum = 0; struct ifreq buf[1 ...

  6. struct ifconf和struct ifreq,获取网线插入状态

    这两天看用C获取当前网口的插入网线状态的程序,遇见了这两个不熟悉的结构体,看了头文件中的说明和详细. struct ifreq 这个结构定义在include/net/if.h,用来配置ip地址,激活接 ...

  7. struct ifreq结构体与ip,子网掩码,网关等信息

    总结一下,今天学习的关于通过socket,ioctl来获得ip,netmask等信息,其中很多内容参照了很多网上的信息,我会一一列出的 我用的这个函数,就是下面这个函数,其中的有一些全局变量,很好懂, ...

  8. C语言实现Windows下获取IP和MAC地址。

    C语言实现Windows下获取IP和MAC地址. #include <winsock2.h> #include <stdio.h> #include <stdlib.h& ...

  9. C# 获取本机CPU序列号,MAC地址,硬盘ID,本机IP地址,计算机名,物理内存,PC类型

    首先引入服务 然后 调用 本文转载自http://blog.sina.com.cn/s/blog_7eeb43210101hf7f.html public class Computer { publi ...

随机推荐

  1. javascript reduce 前端交互 总计

    sum(){ return this.products.reduce((total,next)=>{ return total + next.price * next.aumout},0) } ...

  2. 使用redis事物解决stringRedisTemplate.setIfAbsent()并设置过期时间遇到的问题

    spring-date-redis版本:1.6.2场景:在使用setIfAbsent(key,value)时,想对key设置一个过期时间,同时需要用到setIfAbsent的返回值来指定之后的流程,所 ...

  3. 运输层6——TCP可靠传输的实现

    目录 1. 以字节为单位的滑动窗口 2. 超时重传时间的选择 写在前面:本文章是针对<计算机网络第七版>的学习笔记 运输层1--运输层协议概述 运输层2--用户数据报协议UDP 运输层3- ...

  4. Windows下无法新建文件夹

    原文链接:https://www.jianshu.com/p/ee3205d5b9c9

  5. zookeeper学习(3)----基本结构和命令行操作命令

    一. 数据结构 https://www.jianshu.com/p/8e322462bcca zookeeper存储结构:以树形结构的方式存储数据 ZooKeeper数据模型的结构整体上可以看作是一棵 ...

  6. 【PAT-一道看着很难的水题】L2-023. 图着色问题

    水题!没其他想说的,还以为可以搞点高大上的搜索呢!十五分钟,暴力两重循环就OK了! 代码如下: #include<iostream> #include<stdio.h> #in ...

  7. pygame无法自动补全解决方法

    在pycharm中导入pygame 1.  如果出现 AttributeError: module 'pip' has no attribute 'main'问题 找到安装目录下 helpers/pa ...

  8. JavaScript this 的指向问题

    原文作者:SegmentFault ——写bug 原文链接:https://segmentfault.com/a/1190000015438195 this的指向已经是一个老生常谈的问题,每逢面试都要 ...

  9. linux多jdk切换环境

    临时更改 export JAVA_HOME="/opt/soft/jdk/jdk1.8.0_66" export JRE_HOME="$JAVA_HOME/jre&quo ...

  10. BZOJ 3749: [POI2015]Łasuchy(贪心)

    Orz大佬博客 CODE #include <bits/stdc++.h> using namespace std; typedef long long LL; char cb[1< ...