Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IPSet without automatic summarizing #172

Open
nickzxcv opened this issue Apr 19, 2018 · 1 comment
Open

IPSet without automatic summarizing #172

nickzxcv opened this issue Apr 19, 2018 · 1 comment

Comments

@nickzxcv
Copy link

Like in #1 I'd like to have a data structure containing my actual subnets and its pretty confusing in the documents http://netaddr.readthedocs.io/en/latest/api.html#ip-sets because it talks all about subnets but whenever I add my subnets to an IPSet they get summarized.

I'm working on a script to load information from my networking monitoring system to IPAM so I want the IPAM to handle the summarizing. I can just use a plain python list (also as shown in issue 1) but then don't have all the useful set comparison abilities of netaddr IPSets.

@tpickle-py
Copy link

>>> iplist = [IPAddress('10.0.0.10'),IPAddress('10.0.0.11'),IPNetwork('10.0.1.0/24'),]
>>> new_ip = IPAddress('10.0.0.13')

since the ip list has ip networks and addresses checking if something needs to be updated in IPAM can be as simple as checking if its in the list

>>> ipset =  IPSet(addr for addr in iplist)
>>>
>>> if not new_ip in ipset:
...     print('Notify IPAM')
...     ipset.add(new_ip)
... else:
...     print('We have it inventoried, need to update, check IPAM')
...
Notify IPAM
>>> if not new_ip in ipset:
...     print('Notify IPAM')
...     ipset.add(new_ip)
... else:
...     print('We have it inventoried, need to update, check IPAM')
...
We have it inventoried, need to update, check IPAM

if you need to itter the IP addresses from your lists from sets: try this code

from netaddr import *
for network in ipset:
    if isinstance(network,IPAddress):
        print(network)
    else:
        for ip in network:
            print(ip)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants