Tiny program for listing network interface names and indexes in Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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