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. JVM系列一:虚拟机内存区域

    虚拟机栈 1.虚拟机栈维护一个线程中所有方法的栈帧,每个栈帧中保存着这个方法中用到的局部变量表,操作数栈,常量引用 2.可以用-Xss来设置每个线程中虚拟机栈的大小,在jdk1.4之前默认虚拟机栈大小 ...

  2. python BeautifulSoup4解析网页

    html = """ <html><head><title>The Dormouse's story</title>< ...

  3. 容器上使用Docker还是Rocket?为什么不一起用呢?

    在2014年接近尾声时,CoreOS在Docker发布了Rocket,声称Docker的流程模型有着“根本性的缺陷”.虽然CoreOS的创始人Alex Polvi尽量缓和语气,但是他坚持自己的基本观点 ...

  4. Robot Framework--接口测试中常见的四种POST方式

    写接口测试用例时,遇到以json格式提交数据时,报错,Request如下图: Response如下图: 改成form格式提交,可以正常运行,如下图: 代码如下: ------------------- ...

  5. FreeMarker生成word

    FreeMarker生成word数据填充是通过,Map填充. Map dataMap = new HashMap<String, Object>(); List<User> l ...

  6. tomcat 处理HTTP请求

    一.Tomcat是什么?Tomcat是一个Web应用服务器,同时也是一个Servlet/JSP容器.Tomcat作为Servlet容器,负责处理客户端请求,把请求传送给Servlet,并将Servle ...

  7. Vue之nextTick()

    我们有时候操作 DOM,是想在 data 数据变更的时候进行操作. 那么,我们应该怎么做呢? index.html <!DOCTYPE html> <html lang=" ...

  8. Educational Codeforces Round 75 (Rated for Div. 2) C. Minimize The Integer

    链接: https://codeforces.com/contest/1251/problem/C 题意: You are given a huge integer a consisting of n ...

  9. Educational Codeforces Round 75 (Rated for Div. 2) A. Broken Keyboard

    链接: https://codeforces.com/contest/1251/problem/A 题意: Recently Polycarp noticed that some of the but ...

  10. redis默认端口6379以其名命名,是我孤陋寡闻了,是名性感美女(梅尔兹)

    阿莱西亚-梅尔兹Alessia Merz ,这位亚平宁半岛的性感女人,自从结婚之后就逐渐的淡出了人们的实现,曾经的尤文教母已经现在已经是两个孩子的母亲,但是最近的梅尔兹开始蠢蠢欲动,在相夫教子的同时, ...