Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
crunching ip addresses
#1
Learning python with day to day stuff I normally do in other languages. I am parsing a netstat dump file to get a sorted unique list of remote IPs. I get the ip addresses from a text file and read them into an array (line by line).
x.xx.xxx.xxx
yy.yy.yyy.yyy
etc.

I found code that sorts the array:
import ipaddress
new_list = []
for element in IP_Array:
    new_list.append(ipaddress.ip_address(element))
new_list.sort()
but the output is: [IPv4Address('x.xx.xx.xxx'), ...

The sorting is correct but the output is more than just the IP address. How do I get the output array just the IP address without [IPv4Address('') so I can remove duplicates in the next step?
buran write Nov-10-2020, 08:47 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
what you have is list of IPv4Address objects. When you print a list you get the representation (i.e. __repr__()) of the individual elements in the list.
It looks like you expect to get a str, (i.e. __str__()) for each element

import ipaddress
spam = [ipaddress.ip_address('192.168.0.1'), ipaddress.ip_address('192.168.0.2')]
eggs = [str(ipadd) for ipadd in spam]
print(spam)
print(eggs)
for ipadd in spam:
    print(ipadd)
Output:
[IPv4Address('192.168.0.1'), IPv4Address('192.168.0.2')] ['192.168.0.1', '192.168.0.2'] 192.168.0.1 192.168.0.2

(Nov-09-2020, 03:08 PM)snichols Wrote: so I can remove duplicates in the next step

You don't need to do so, to remove the duplicates

import ipaddress
spam = [ipaddress.ip_address('192.168.0.1'), ipaddress.ip_address('192.168.0.2'), ipaddress.ip_address('192.168.0.1')]
print(spam)
print(list(set(spam)))
Output:
[IPv4Address('192.168.0.1'), IPv4Address('192.168.0.2'), IPv4Address('192.168.0.1')] [IPv4Address('192.168.0.2'), IPv4Address('192.168.0.1')]
Note, set is unordered collections, so no guarantees about the order
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
From buran's answer one could easily conclude that in order to get sorted unique ip addresses one should first uniquify and then sort. As sorted() takes any iterable and returns list one can just apply it to set and get sorted list:

>>> import ipaddress
>>> spam = [ipaddress.ip_address('192.168.0.1'), ipaddress.ip_address('192.168.0.2'), ipaddress.ip_address('192.168.0.1')]
>>> sorted(set(spam))
[IPv4Address('192.168.0.1'), IPv4Address('192.168.0.2')]
As far as conversion goes:

from Python 3.9 there is also IPv4Address.__format__(fmt) for converting into other types (string, binary, hex).

Similarly to str() there is also possibility to convert to integer with int()
buran likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(Nov-10-2020, 09:22 AM)perfringo Wrote: in order to get sorted unique ip addresses one should first uniquify and then sort.
I didn't go into details about sorting, because from OP it looked like a means to find the unique ip addresses.

(Nov-10-2020, 09:22 AM)perfringo Wrote: from Python 3.9 there is also IPv4Address.__format__(fmt) for converting into other types (string, binary, hex).

great point, just to add that normally .__format__() is not meant to be invoked directly. I will just copy the example from the docs
>>> format(ipaddress.IPv4Address('192.168.0.1'))
'192.168.0.1'

>>> '{:#b}'.format(ipaddress.IPv4Address('192.168.0.1'))
'0b11000000101010000000000000000001'

>>> f'{ipaddress.IPv6Address("2001:db8::1000"):s}'
'2001:db8::1000'

>>> format(ipaddress.IPv6Address('2001:db8::1000'), '_X')
'2001_0DB8_0000_0000_0000_0000_0000_1000'

>>> '{:#_n}'.format(ipaddress.IPv6Address('2001:db8::1000'))
'0x2001_0db8_0000_0000_0000_0000_0000_1000'
DeaD_EyE likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
I often use the fact, that a dict is a set without values, but since Python 3.6 the order is preserved.
So using list(dict.fromkey(iterable)) will keep the order from the original iterable, but will remove duplicates.

from ipaddress import ip_address

spam = [ip_address('192.168.0.1'), ip_address('192.168.0.2'), ip_address('192.168.0.1')]
unique_preserved_order = list(dict.fromkeys(spam))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
Thanks all for the info. I have my code de-duped and sorted. This is the first thing I have tried with python and there is so much to it, but I am liking it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  a function to get IP addresses of interfaces Skaperen 2 1,380 May-30-2022, 05:00 PM
Last Post: Skaperen
  Loop through list of ip-addresses [SOLVED] AlphaInc 7 3,840 May-11-2022, 02:23 PM
Last Post: menator01
  instance methods sharing addresses mim 1 2,207 Mar-28-2021, 05:22 AM
Last Post: deanhystad
  Convert email addresses to VCF format jehoshua 2 4,610 Mar-06-2021, 12:50 AM
Last Post: jehoshua
  extract email addresses from gmail vigneshboolog 0 1,732 Feb-11-2020, 09:23 AM
Last Post: vigneshboolog
  Extract email addresses from string and store them in a list oslosurfer 2 2,631 Nov-24-2019, 03:35 PM
Last Post: oslosurfer
  Read list of IP addresses from file and return a network dflick 4 4,803 Oct-27-2018, 09:33 AM
Last Post: buran
  Help required to print MAC addresses anna 50 20,202 Feb-22-2018, 09:53 AM
Last Post: anna
  How do I get coordinates for addresses from the csv list? BigD 6 7,744 Dec-02-2017, 09:38 PM
Last Post: BigD
  How to get data instead of memory addresses KBingo 3 14,085 Jun-12-2017, 12:36 AM
Last Post: KBingo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020