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; }