c/c++ 网络编程 UDP 改变网卡的硬件地址
程序员文章站
2022-03-03 08:42:53
网络编程 UDP 改变网卡的硬件地址 在程序里动态改变网卡的硬件地址 1,取得网卡的硬件地址 "github源代码" 2,改变网卡的硬件地址 "github源代码" c/c++ 学习互助QQ群:877684253 本人微信:xiaoshitou5854 ......
网络编程 udp 改变网卡的硬件地址
在程序里动态改变网卡的硬件地址
1,取得网卡的硬件地址
#include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <net/if.h> int main(){ int fd; ifreq ifr; fd = socket(af_inet, sock_dgram, 0); strncpy(ifr.ifr_name, "enp0s3", ifnamsiz - 1); if(ioctl(fd, siocgifhwaddr, &ifr) != 0){ perror("ioctl"); return 1; } close(fd); printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", (unsigned char)ifr.ifr_hwaddr.sa_data[0], (unsigned char)ifr.ifr_hwaddr.sa_data[1], (unsigned char)ifr.ifr_hwaddr.sa_data[2], (unsigned char)ifr.ifr_hwaddr.sa_data[3], (unsigned char)ifr.ifr_hwaddr.sa_data[4], (unsigned char)ifr.ifr_hwaddr.sa_data[5]); }
2,改变网卡的硬件地址
#include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <net/if.h> #include <net/if_arp.h> int main(){ int fd; ifreq ifr; fd = socket(af_inet, sock_dgram, 0); ifr.ifr_hwaddr.sa_family = arphrd_ether; ifr.ifr_hwaddr.sa_data[0] = 0xaa; ifr.ifr_hwaddr.sa_data[1] = 0xbb; ifr.ifr_hwaddr.sa_data[2] = 0xcc; ifr.ifr_hwaddr.sa_data[3] = 0xdd; ifr.ifr_hwaddr.sa_data[4] = 0xee; ifr.ifr_hwaddr.sa_data[5] = 0xff; strncpy(ifr.ifr_name, "enp0s3", ifnamsiz - 1); if(ioctl(fd, siocsifhwaddr, &ifr) != 0){ perror("ioctl"); return 1; } }
c/c++ 学习互助qq群:877684253
本人微信:xiaoshitou5854
下一篇: C++基础 对象的管理——单个对象的管理