可以实现的,使用ioctl函数,加上想获得的选项即可。
正好手头有个类似的例子:
=======================================================
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXINTERFACES 16
int main(int argc, char **argv){
register int fd, interface, retn = 0;
struct ifreq buf[MAXINTERFACES];
struct arpreq arp;
struct ifconf ifc;
char mac[32] = "";
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) { ifc.ifc_len = sizeof buf;
ifc.ifc_buf = (caddr_t) buf;
if (!ioctl(fd, SIOCGIFCONF, (char *) &ifc)) {
interface = ifc.ifc_len / sizeof(struct ifreq);
printf("interface num is interface=%d\n\n", interface);
while (interface-- > 0) {
printf("net device %s\n", buf[interface].ifr_name);
/*Jugde whether the net card status is promisc */
if (!(ioctl(fd, SIOCGIFFLAGS, (char *) &buf[interface]))) { if (buf[interface].ifr_flags & IFF_PROMISC) {
printf("the interface is PROMISC");
return 0;
}
} else {
char str[256] = "";
sprintf(str, "cpm: ioctl device %s",buf[interface].ifr_name);
perror(str);
}
/*Jugde whether the net card status is up */ if (buf[interface].ifr_flags & IFF_UP) {
printf("the interface status is UP\n");
} else {
printf("the interface status is DOWN\n");
}
/*Get IP of the net card */ if (!(ioctl(fd, SIOCGIFADDR, (char *) &buf[interface]))) {
printf("IP address is:");
printf("%s\n", inet_ntoa(((struct sockaddr_in*) (&buf[interface].ifr_addr))->sin_addr));
} else {
char str[256] = "";
sprintf(str, "cpm: ioctl device %s",buf[interface].ifr_name);
perror(str);
}
/*Get HW ADDRESS of the net card */ if (!(ioctl(fd, SIOCGIFHWADDR, (char *) &buf[interface]))) {
printf("HW address is:");
sprintf(mac, "%02x%02x%02x%02x%02x%02x",
(unsigned char) buf[interface].ifr_hwaddr.sa_data[0],
(unsigned char) buf[interface].ifr_hwaddr.sa_data[1],
(unsigned char) buf[interface].ifr_hwaddr.sa_data[2],
(unsigned char) buf[interface].ifr_hwaddr.sa_data[3],
(unsigned char) buf[interface].ifr_hwaddr.sa_data[4],
(unsigned char) buf[interface].ifr_hwaddr.sa_data[5]); // 利用sprintf转换成char *
printf("%s\n", mac);
printf("\n");
}
else {
char str[256];
sprintf(str, "cpm: ioctl device %s",buf[interface].ifr_name);
perror(str);
}
} //end of while
} else
perror("cpm: ioctl");
} else
perror("cpm: socket");
close(fd);
return retn;
}
==============================================
输出:
[root@temp]$./deleteme interface num is interface=2
net device eth0the interface status is UP
IP address is:10.6.15.102
HW address is:005056a44485
net device lothe interface status is UP
IP address is:127.0.0.1
HW address is:000000000000