[Nagiosplug-devel] check_dhcp - a possible addition

Stephan Janosch stephan.janosch at interface-business.de
Tue Jan 18 08:01:33 CET 2005


Hi folks!

Fizzeling(freebsd has no eth0, O_o ) around with check_dhcp I figured 
out, that I have to give an interface name. Perhaps you can include the 
code at the bottom of this message. Basicly the code looks through the 
interface table and searches for interfaces which are up and can broadcast.

This could make the use a bit more flexible.

regards, Stephan

btw: I'm not subscribed at nagiosplug-devel

---------------------------------------------------------------------------------
#include <sys/types.h>
#include <sys/socket.h>

#include <net/if.h>

#include <err.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(void)
{
   struct ifaddrs *iflist, *walker;
   char **namelist = NULL, *cp;
   int nitems = 0, i, found;
   size_t l;

   if (getifaddrs(&iflist) != 0)
     err(1, "getifaddrs()");

   for (walker = iflist; walker != NULL; walker = walker->ifa_next)
     if ((walker->ifa_flags & (IFF_UP | IFF_BROADCAST)) ==
	(IFF_UP | IFF_BROADCAST))
       {
	for (i = found = 0; i < nitems; i++)
	  if (strcmp(namelist[i], walker->ifa_name) == 0)
	    {
	      found++;
	      break;
	    }
	if (found)
	  continue;

	nitems++;
	if ((namelist = realloc(namelist, nitems * sizeof(char *))) ==
	    NULL)
	  abort();
	l = strlen(walker->ifa_name);
	if ((cp = malloc(l + 1)) == NULL)
	  abort();
	strcpy(cp, walker->ifa_name);
	namelist[nitems - 1] = cp;
       }
   freeifaddrs(iflist);

   if (nitems == 0)
     {
       printf("No interfaces.\n");
       return 0;
     }
   printf("%d interface%s:", nitems, nitems > 1? "s": "");
   for (i = 0; i < nitems; i++)
     {
       if (i > 0)
	printf(",");
       printf(" %s", namelist[i]);
       free(namelist[i]);
     }
   printf("\n");
   free(namelist);

   return 0;
}





More information about the Devel mailing list