Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux getting all network interface names

I need to collect all the interface names, even the ones that aren't up at the moment. Like ifconfig -a.

getifaddrs() is iterating through same interface name multiple times. How can I collect all the interface names just once using getifaddrs()?

like image 314
tez Avatar asked Oct 07 '13 14:10

tez


People also ask

How do I get a list of network interfaces in Linux?

You can use the ifconfig command to list the network interfaces available in your system. Instead of typing ifconfig, type the command /sbin/ifconfig to list the network interfaces in your system.

How can I see all network interfaces?

Open the terminal application. Type ifconfig -a to check the network interface. Press Enter to run the command. The output will display information about all the network interfaces on the system.

What command will display all of the Ethernet interfaces within Linux?

ifconfig command – It is used to display or configure a network interface.

How do I find the number of NICS in Linux?

Here is what you need to do. First list all NIC ports, each line is a port. Now you can see the bus-info: 0000:08:00.0 matches 08:00.0 Ethernet controller: Solarflare Communications SFC9120 (rev 01) . Now you will be able to find out how many NIC ports you have, how many free ports on each card.


1 Answers

You could check which entries from getifaddrs belong to the AF_PACKET family. On my system that seems to list all interfaces:

struct ifaddrs *addrs,*tmp;

getifaddrs(&addrs);
tmp = addrs;

while (tmp)
{
    if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_PACKET)
        printf("%s\n", tmp->ifa_name);

    tmp = tmp->ifa_next;
}

freeifaddrs(addrs);
like image 90
brm Avatar answered Oct 06 '22 00:10

brm