[python]Excluding ip addresses belonging a subnet

suppose if you need to enumerate ip addresses from a list, but want to exclude ip addresses belonging to a subnet here’s a solution which you can do with netaddr module.
Supposed ip_from_inventory is the list of ip addresses you have obtained from a file.

from netaddr import IPNetwork, IPAddress
for ip_item in ip_from_inventory:
    if IPAddress(ip_item) not in IPNetwork('').iter_hosts():

This example exclude ip address that belongs to, and will list other ip addresses that are not.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s