Skip to content

Instantly share code, notes, and snippets.

@provegard
Created November 24, 2011 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save provegard/1392469 to your computer and use it in GitHub Desktop.
Save provegard/1392469 to your computer and use it in GitHub Desktop.
Tiny program for listing network interface names and indexes in Linux
/* Tiny program for listing network interface names and indexes in Linux.
*
* Compile with: gcc -std=c99 -o listifs listifs.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <net/if.h>
int main(int argc, char *argv[])
{
struct if_nameindex *array;
if ((array = if_nameindex()) == NULL) {
perror("if_nameindex");
exit(EXIT_FAILURE);
}
for (int i = 0; array[i].if_name != NULL; i++) {
printf("name = %s, index = %d\n",
array[i].if_name,
array[i].if_index);
}
if_freenameindex(array);
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment