So I have Python code which creates a Linux TAP interface, but it always comes up with a random, auto-generated MAC address. To simplify testing I would like to manually assign the interface a MAC address after it has been initialized. I know I can do this with the SIOCSIFHWADDR ioctl and struct ifreq, but I had a few questions about the implementation.
Here is the C struct:
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;
};
Now I think that I only need to fill in the hwaddr field, but this being a union I am not sure how to create the structure in Python.
What is the best way to call the SIOCSIFHWADDR ioctl with struct ifreq to change an interface’s MAC address in Python?
Thanks!
from: http://nullege.com/codes/show/src@p@y@pynetlinux-1.0@pynetlinux@ifconfig.py
I’ve used a lot of the networking methods from that module; very useful!