zoukankan      html  css  js  c++  java
  • struct ifreq 获取IP 和mac和修改mac

    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 ",  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 ",                                   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 ",   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 ", 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! ");    close(m_socket);    return 0;    }    fprintf(stderr, "config mac address failed! ");    close(m_socket); } #else char cmd[128] = {''}; 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 ", cmd); system(cmd); #endif return -1; }

     
  • 相关阅读:
    [j2me]KSoap2在Nokia真机上可能导致SymbianOS error 28
    [J2ME]Nokia播放音乐时发生MediaException的解决办法
    [j2me]利用JLayerMECLDC0.2播放MP3的试验[1]
    [j2me]手机也可以玩播客(Podcast)! Geek开发说明[开源]
    [JavaME]手机申请移动分配的动态IP(3)?
    [流媒体]实例解析MMS流媒体协议,下载LiveMediaVideo[1][修正版,增加了带宽测试包]
    [JavaME]手机申请移动分配的动态IP(2)?
    [JavaME]手机是否能够申请到动态IP?
    [收藏]MIDlet如何签名的tutorial
    [流媒体]实例解析MMS流媒体协议,下载LiveMediaVideo[3]
  • 原文地址:https://www.cnblogs.com/tianciliangen/p/3456192.html
Copyright © 2011-2022 走看看