0

Hi I am trying to get the IP of every interface on my machine using netifaces in python. I am still new to python and working my way through some concepts but it seems like I can query every NIC at once and then put the results into a list the problem is my end goal is to ask the user which network to work on and I want to exclude everything that returns 'No IP addr'.

I have already tried a few different methods including removing strings from the list and that didnt work or only adding IP addresses objects but im pretty sure im not doing it properly since it still errors out. Any help is appreciated.

import os
import socket
from netifaces import interfaces, ifaddresses, AF_INET

def get_self_IP():
    for ifaceName in interfaces():
        addresses = []
        possibilities = [i['addr'] for i in ifaddresses(ifaceName).setdefault(AF_INET, [{'addr':'No IP addr'}] )]
        print(' '.join(possibilities))
        for i in possibilities:
            if isinstance(i, ifaddress.ipaddress): # if i is an IP address
                addresses.append(i)
        print(addresses)

Also I have two lists now because ive changed it a few times to troubleshoot but if I can keep it as one list and only .append the IPs to it while its gathering IPs rather than have to do an entire separate list with a separate for or if loop it would be ideal but I do not know another way unfortunately.

  • Did you mean to put the line: `addresses = []` just above the `for` loop? And then perhaps `return addresses` as the last line? – quamrana Apr 09 '22 at 12:13
  • originally I had it above the for loop but I've been moving stuff around to troubleshoot I apologize. The issue is more about how to remove/exclude the unwanted results from `possibilities = [i['addr'] for i in ifaddresses(ifaceName).setdefault(AF_INET, [{'addr':'No IP addr'}] )]` – sam.solo.works Apr 09 '22 at 12:53

1 Answers1

1

Not sure if this is what you want, but:

I changed the function to:

def get_self_IP():
  addresses = []
  for ifaceName in interfaces():
      ifaddresses_list = ifaddresses(ifaceName)
      possibilities = [i['addr'] for i in ifaddresses_list.setdefault(AF_INET, [{'addr': 'No IP addr'}])]
      for i in possibilities:
          if ifaddresses_list[2][0]['addr'] != "No IP addr":
              addresses.append(i)
  print(addresses)

The ifaddresses(ifaceName) returns a dictionary which apparently the key 2 contains the IP address in the index 0 with the key 'addr', which in case the IP doesn't exist the value is No IP addr. In that the if checks if that is not the case and then adds the IP address to the addresses list.

If I didn't understand what you want clearly, please answer with more details :)

d3lux1999
  • 68
  • 7
  • 1
    Yea that works perfectly thank you! Can I ask how you found out that that object returned with 3 indexes? Just so I know how to better troubleshoot in the future? – sam.solo.works Apr 11 '22 at 12:48
  • 1
    Not sure if I remember the original output (can't run the code now), but it was some type of nested dictionary or a dictionary inside a list. If you want to take the steps and reach the answer, just print the `ifaddresses_list` without any index and then start to dig down adding those indexes. EDIT: I just remembered that the first index `[2]` is actually a dictionary key, not the position 2. Just so it doesn't confuse you. – d3lux1999 Apr 11 '22 at 14:04