There are various ways for listing the network interfaces while programming in C, The below code sample should be straight forward & list them all.


#include<stdio.h> #include<net/if.h> #include<string.h> int main(int argc, char *argv[]){ // Initializing Some Variables unsigned int max_interfaces = 255; char ifname[IFNAMSIZ]; char old_ifname[IFNAMSIZ]; for (unsigned int if_id=0; if_id<max_interfaces; if_id++) { if_indextoname(if_id, ifname); if (ifname[0] == '\0'){ continue; } // Breaking out when the last interface name starts repeating else if (strcmp(ifname, old_ifname) == 0){ break; } // Printing the interace id and name in stdout & storing the current value as a reference for the next iteration else { printf("%d: %s\n", if_id, ifname); snprintf(old_ifname, sizeof(old_ifname), "%s", ifname); } } return 0; }

This code is compiled & executed on linux o a recent kernel version, Please feel free to test it on other systems.

example:


user@pc$ gcc list_interfaces.c -o list_interfaces user@pc$ ./list_interfaces 0: abc 1: lo 2: wlp6s0 3: enp2s0 4: docker0 5: testnet 6: vboxnet0 7: vboxnet1 8: vboxnet2 9: vboxnet3 10: eth_dummy

[amazon_link asins=’B01H0LBF9Q,0321776410,9332554668,1973911817,1530891183,1593279000′ template=’ProductCarousel’ store=’ipvx-21′ marketplace=’IT’ link_id=’c398ce10-98f9-11e8-b4d9-f1a2a813a28e’]