Skip to content

Instantly share code, notes, and snippets.

@mad4j
Created February 7, 2014 14:54
Show Gist options
  • Save mad4j/8864135 to your computer and use it in GitHub Desktop.
Save mad4j/8864135 to your computer and use it in GitHub Desktop.
How to change MAC using ioctl
#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