#include <ctype.h>;
#include <err.h>;
#include <errno.h>;
#include <stdio.h>;
#include <stdlib.h>;
#include <string.h>;
#include <unistd.h>;
#include <sys/ioctl.h>;
#include <sys/socket.h>;
#include <net/if.h>;
#include <net/if_types.h>;
#include <net/if_media.h>;
int
main(int argc,char **argv)
{
int s;
struct ifreq ifr;
struct ifmediareq ifmr;
int *media_list;
char name[IFNAMSIZ];
if(2 != argc){
printf("Usage:/ntest cardname(e.g: vr0)/n");
return 1;
}
strncpy(name, argv[1], sizeof(name));
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
err(1, "socket");
(void) memset(&ifmr, 0, sizeof(ifmr));
(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
err(1, "SIOCGIFMEDIA");
if (ifmr.ifm_count == 0) {
warnx("%s: no media types?", name);
return;
}
media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
if (media_list == NULL)
err(1, "malloc");
ifmr.ifm_ulist = media_list;
if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
err(1, "SIOCGIFMEDIA");
if (ifmr.ifm_status & IFM_AVALID) {
printf("/tstatus: ");
switch (IFM_TYPE(ifmr.ifm_active)) {
case IFM_ETHER:
if (ifmr.ifm_status & IFM_ACTIVE)
printf("active");
else
printf("no carrier");
break;
case IFM_FDDI:
case IFM_TOKEN:
if (ifmr.ifm_status & IFM_ACTIVE)
printf("inserted");
else
printf("no ring");
break;
case IFM_ATM:
if (ifmr.ifm_status & IFM_ACTIVE)
printf("active");
else
printf("no carrier");
break;
case IFM_IEEE80211:
if (ifmr.ifm_status & IFM_ACTIVE)
printf("associated");
else
printf("no carrier");
break;
}
putchar('/n');
}
free(media_list);
return 0;
}
[/code]
firewall# gcc test.c -o test
firewall# test vr0
status: active
firewall# test vr1
status: active
firewall# test vr2
test: SIOCGIFMEDIA: Device not configured
网络在线测试2
最新推荐文章于 2021-05-12 12:28:10 发布