日期:2014-05-16 浏览次数:20793 次
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <sys/ioctl.h> #include <net/if_arp.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> char *ip = "192.168.205.168"; int main() { struct arpreq arp; /**/ struct sockaddr_in * paddr = (struct sockaddr_in *)&arp.arp_pa;; int s, ret = 0; char *mac = arp.arp_pa.sa_data; s = socket(AF_INET, SOCK_STREAM, 0); paddr->sin_family = AF_INET; paddr->sin_port = htons(9090); paddr->sin_addr.s_addr = inet_addr(ip); strcpy(arp.arp_dev, "eth0"); ret = ioctl(s, SIOCGARP, &arp); if(ret == -1) { perror("ioctl SIOCGARP"); exit(1); } printf("%2x-%2x-%2x-%2x-%2x-%2x\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]); return 0; }