Skip to content

Instantly share code, notes, and snippets.

@j1ah0ng
Last active March 23, 2020 06:37
Show Gist options
  • Save j1ah0ng/e1a29d0fff3513764b8d6dce0d454b03 to your computer and use it in GitHub Desktop.
Save j1ah0ng/e1a29d0fff3513764b8d6dce0d454b03 to your computer and use it in GitHub Desktop.
quick little program that takes a network interface at argv[1] and gets the Linux interface id
#include <errno.h>
#include <net/if.h>
#include <stdlib.h>
#include <stdio.h>
#ifndef NULL
#define NULL 0
#endif
int main(int argc, char *argv[]) {
char *name = (argc < 2) ? "eth0" : argv[1];
unsigned int idx = if_nametoindex(name);
if (idx == 0) {
printf("Oh no THAT WAS NOT A VALID INTERFACE the world shall end and thus errno was set to %d!\n", errno);
}
else
printf("CONGRATULATIONS you win the lottery for the index of %s was %d.\n", name, idx);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment