Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 461
Wilson F Avatar asked Nov 06 '08 23:11

Wilson F


People also ask

How do you find all IP's on a network?

To see all of the devices connected to your network, type arp -a in a Command Prompt window. This will show you the allocated IP addresses and the MAC addresses of all connected devices.

Does each NIC have an IP address?

By default, each network interface card (NIC) has its own unique IP address. However, you can assign multiple IP addresses to a single NIC.

Can two NICs have same IP?

You won't "give both ports the same IP address" since there will only be one "port" visible to give an IP address to. Show activity on this post. Each "LAN port", or NIC, must have a unique IP address. It would be unusual for each of the IPs to be on the same network.

Can a device have multiple NICs?

Each network interface controller (NIC) in a computer has a unique medium access control (MAC) address. If multiple NICs are installed in one computer, they each have their own MAC addresses.


2 Answers

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

>>> import netifaces >>> netifaces.interfaces() ['lo', 'eth0'] >>> netifaces.ifaddresses('eth0') {17: [{'broadcast': 'ff:ff:ff:ff:ff:ff', 'addr': '00:11:2f:32:63:45'}], 2: [{'broadcast': '10.0.0.255', 'netmask': '255.255.255.0', 'addr': '10.0.0.2'}], 10: [{'netmask': 'ffff:ffff:ffff:ffff::', 'addr': 'fe80::211:2fff:fe32:6345%eth0'}]} >>> for interface in netifaces.interfaces(): ...   print netifaces.ifaddresses(interface)[netifaces.AF_INET] ... [{'peer': '127.0.0.1', 'netmask': '255.0.0.0', 'addr': '127.0.0.1'}] [{'broadcast': '10.0.0.255', 'netmask': '255.255.255.0', 'addr': '10.0.0.2'}] >>> for interface in netifaces.interfaces(): ...   for link in netifaces.ifaddresses(interface)[netifaces.AF_INET]: ...     print link['addr'] ... 127.0.0.1 10.0.0.2 

This can be made a little more readable like this:

from netifaces import interfaces, ifaddresses, AF_INET  def ip4_addresses():     ip_list = []     for interface in interfaces():         for link in ifaddresses(interface)[AF_INET]:             ip_list.append(link['addr'])     return ip_list 

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.

like image 185
Harley Holcombe Avatar answered Sep 24 '22 19:09

Harley Holcombe


import socket [i[4][0] for i in socket.getaddrinfo(socket.gethostname(), None)] 
like image 32
Nakilon Avatar answered Sep 26 '22 19:09

Nakilon