Skip to content
Advertisement

How do I determine all of my IP addresses when I have multiple NICs?

I have multiple Network Interface Cards on my computer, each with its own IP address.

When I use gethostbyname(gethostname()) from Python’s (built-in) socket module, it will only return one of them. How do I get the others?

Advertisement

Answer

Use the netifaces module. Because networking is complex, using netifaces can be a little tricky, but here’s how to do what you want:

JavaScript

This can be made a little more readable like this:

JavaScript

If you want IPv6 addresses, use AF_INET6 instead of AF_INET. If you’re wondering why netifaces uses lists and dictionaries all over the place, it’s because a single computer can have multiple NICs, and each NIC can have multiple addresses, and each address has its own set of options.

Advertisement