-
-
Save xiangruipuzhao/923de4b2361a9bb073720dbce1ef0ccf to your computer and use it in GitHub Desktop.
How to change MAC using ioctl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include | |
| #include <sys/ioctl.h> | |
| #include | |
| #include | |
| #include <sys/types.h> | |
| #include <sys/socket.h> | |
| #include <net/if_arp.h> | |
| #include <net/if.h> | |
| #include | |
| #include | |
| int main(int argc, char **argv) { | |
| struct ifreq ifr; | |
| int s; | |
| char mac_char[] = "12:34:56:78:12:34"; | |
| sscanf(mac_char, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", | |
| &ifr.ifr_hwaddr.sa_data[0], | |
| &ifr.ifr_hwaddr.sa_data[1], | |
| &ifr.ifr_hwaddr.sa_data[2], | |
| &ifr.ifr_hwaddr.sa_data[3], | |
| &ifr.ifr_hwaddr.sa_data[4], | |
| &ifr.ifr_hwaddr.sa_data[5] | |
| ); | |
| s = socket(AF_INET, SOCK_DGRAM, 0); | |
| assert(s != -1); | |
| strcpy(ifr.ifr_name, "eth0"); | |
| ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; | |
| assert(ioctl(s, SIOCSIFHWADDR, &ifr) != -1); | |
| return EXIT_SUCCESS; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment