Skip to content

Instantly share code, notes, and snippets.

@Gumnos
Created January 4, 2022 15:47
Show Gist options
  • Save Gumnos/0de5e0bdef08d813f94d4b2b6f71f684 to your computer and use it in GitHub Desktop.
Save Gumnos/0de5e0bdef08d813f94d4b2b6f71f684 to your computer and use it in GitHub Desktop.
A short C program to determine if one or more network interfaces exist on FreeBSD
# compile with `cc -o if_exists dexter_if_exists.c`
#include <stdio.h>
#include <sysexits.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
int
main(int argc, char *argv[]) {
int i;
if (argc == 1) {
fprintf(stderr, "usage: %s [ifname ...]\n", argv[0]);
fprintf(stderr, "returns 0 if all ifname exist\n");
return EX_USAGE;
} else {
for (i=1; i<argc; i++)
if (if_nametoindex(argv[i]) == 0) return 1;
return EX_OK;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment