Skip to content

Instantly share code, notes, and snippets.

@saivert
Created February 4, 2023 21:36
Show Gist options
  • Select an option

  • Save saivert/ca083e9bea0e2beb03b3e5b427970e52 to your computer and use it in GitHub Desktop.

Select an option

Save saivert/ca083e9bea0e2beb03b3e5b427970e52 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netlink/route/link.h>
int main(int argc, char *argv[])
{
struct rtnl_link *link;
struct nl_sock *sk;
int err;
char *linkname;
struct nl_dump_params params = {
.dp_type = NL_DUMP_LINE,
.dp_fd = stdout,
};
if (argc != 2) {
printf ("Missing argument!\n");
return 1;
}
linkname = argv[1];
sk = nl_socket_alloc();
if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
nl_perror(err, "Unable to connect socket");
return err;
}
struct nl_cache *link_cache;
rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache);
link = rtnl_link_get_by_name(link_cache, linkname);
if (!link) {
printf("Error: No link with name %s\n", linkname);
return 1;
}
#if 0
struct nl_addr *addr = rtnl_link_get_addr (link);
char buf[256];
printf ("Mac address is: %s\n", nl_addr2str (addr, buf, sizeof(buf)));
#endif
nl_cache_dump_filter (link_cache, &params, OBJ_CAST(link));
printf ("Type: %s\n", rtnl_link_get_type(link));
rtnl_link_put(link);
nl_close(sk);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment