- 论坛徽章:
- 0
|
strace ifconfig就知道用了哪个ioctl了:SIOCGIFHWADDR- #include <stdio.h>
- #include <string.h>
- #include <sys/ioctl.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <net/if.h>
- int getmac(char *ethname, char *macbuf)
- {
- int sock;
- struct ifreq iface;
- char tmp_mac[10];
- sock = socket(PF_INET, SOCK_DGRAM, 0);
- strcpy(iface.ifr_name, ethname);
- if(ioctl(sock, SIOCGIFHWADDR, &iface) != 0) {
- close(sock);
- return 1;
- }
- memcpy(tmp_mac, iface.ifr_hwaddr.sa_data, 6);
- sprintf(macbuf, "%02X:%02X:%02X:%02X:%02X:%02X",
- tmp_mac[0] & 0xFF, tmp_mac[1] & 0xFF, tmp_mac[2] &0xFF,
- tmp_mac[3] & 0xFF, tmp_mac[4] & 0xFF, tmp_mac[5] & 0xFF);
- return 0;
- }
- int main(void)
- {
- char buf[256];
- getmac("eth0", buf);
- puts(buf);
- return 0;
- }
复制代码 |
|