2 * src/nl-link-name2ifindex.c Transform a interface name to its index
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
14 int main(int argc, char *argv[])
16 struct nl_handle *nlh;
17 struct nl_cache *link_cache;
18 int err = -1, ifindex;
20 if (nltool_init(argc, argv) < 0)
23 if (argc < 2 || !strcmp(argv[1], "-h")) {
24 printf("Usage: nl-link-name2ifindex <name>\n");
28 nlh = nltool_alloc_handle();
32 if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
35 link_cache = nltool_alloc_link_cache(nlh);
39 if ((ifindex = rtnl_link_name2i(link_cache, argv[1])) == RTNL_LINK_NOT_FOUND)
40 fprintf(stderr, "Interface %s does not exist\n", argv[1]);
42 printf("%d\n", ifindex);
44 nl_cache_free(link_cache);
48 nl_handle_destroy(nlh);