1

I've been reading about the following solutions here, but it only works for one IP Address only. I'm not able to print the rest of the IPs (multiple network/wireless cards).

References

  1. http://net-informations.com/python/net/ipadress.htm

  2. Finding local IP addresses using Python's stdlib

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

C:\>ipconfig | findstr IPv4
   IPv4 Address. . . . . . . . . . . : 192.168.1.1
   IPv4 Address. . . . . . . . . . . : 192.168.5.1

C:\>python
Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> print (socket.gethostbyname(socket.gethostname()))
192.168.5.1
>>>

Please let me know how to print all IP Addresses in Python.

Update 1:

As suggested by Rahul, I've tried the following code but it didn't return anything on the screen.

c:\Python\Codes>more ip.py
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

c:\Python\Codes>

c:\Python\Codes>ip.py

c:\Python\Codes>

Update 2:

I've also tried Elemag's code as suggested here. It works on Python interpreter but not when I save the code to .py

c:\Python\Codes>python
Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> [netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifa
ces.ifaddresses(iface)]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'netifaces' is not defined
>>>
>>> import netifaces
>>> [netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifa
ces.ifaddresses(iface)]
['192.168.1.10', '192.168.56.1', '127.0.0.1']
>>>
>>> ^Z

It's not working when I save the code into .py

c:\Python\Codes>more test.py
[netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifaces.
ifaddresses(iface)]

c:\Python\Codes>test.py
Traceback (most recent call last):
  File "c:\Python\Codes\test.py", line 1, in <module>
    [netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifa
ces.ifaddresses(iface)]
NameError: name 'netifaces' is not defined

c:\Python\Codes>

c:\Python\Codes>more test.py
import netifaces
[netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr'] for iface in netifaces.interfaces() if netifaces.AF_INET in netifaces.
ifaddresses(iface)]

c:\Python\Codes>

c:\Python\Codes>test.py

c:\Python\Codes>
  • 1. There's no concept of `returning to the screen`. 2. You don't even call function `ip_adresses()` anywhere. Your program does essentially nothing. – ElmoVanKielmo Mar 09 '18 at 15:16
  • Thanks @ElmoVanKielmo I've added `print (ip4_addresses)` but not getting the output I expected. How do I fix this? –  Mar 09 '18 at 15:26

2 Answers2

3

I used the following example from this site as a starting point (changed to work in Python 3): https://yamakira.github.io/python-network-programming/libraries/netifaces/index.html as well as info from the module: https://pypi.org/project/netifaces/

import netifaces
for iface in netifaces.interfaces():
  iface_details = netifaces.ifaddresses(iface)
  if netifaces.AF_INET in iface_details:
    print(iface_details[netifaces.AF_INET])

which returns interface info in dictionary form:

[{'addr': '192.168.0.90', 'netmask': '255.255.255.0', 'broadcast': '192.168.0.255'}]
[{'addr': '127.0.0.1', 'netmask': '255.0.0.0', 'broadcast': '127.255.255.255'}]

as an extension to this, if you do something like:

import netifaces
for iface in netifaces.interfaces():
    iface_details = netifaces.ifaddresses(iface)
    if netifaces.AF_INET in iface_details:
        print(iface_details[netifaces.AF_INET])
        for ip_interfaces in iface_details[netifaces.AF_INET]:
            for key, ip_add in ip_interfaces.items():
                if key == 'addr' and ip_add != '127.0.0.1':
                    print(key, ip_add)

This would return your IP address in my case:

addr 192.168.0.90

Obviously, you can manipulate the dictionary as you see fit, I just added it for information sake

  • tested on windows 10 python 3.6
  • tested on linux python 2.7, 3.6

hope this helps someone

allprog
  • 16,540
  • 9
  • 56
  • 97
IamSierraCharlie
  • 504
  • 5
  • 10
0
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
print(ip4_addresses())

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

rahul
  • 21
  • 7
  • Thanks @Rahul, I tried the code but nothing happen `c:\Python\Codes>more ip.py 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 c:\Python\Codes>ip.py c:\Python\Codes>` –  Mar 09 '18 at 15:01
  • use `print(ip4_addresses())` to print the output. I have updated my code, take a look at it. Thank you – rahul Mar 09 '18 at 15:30
  • 1
    Didn't work. `print(ip4_addresses())` caused the following error. `c:\Python\Codes>ip.py Traceback (most recent call last): File "c:\Python\Codes\ip.py", line 8, in print(ip4_addresses()) File "c:\Python\Codes\ip.py", line 5, in ip4_addresses for link in ifaddresses(interface)[AF_INET]: KeyError: 2 c:\Python\Codes>` –  Mar 09 '18 at 15:37