Export to GitHub

netaddr - NetCIDRMigration.wiki


Background

Updated for release 0.5

NetCIDR was a Python package created several years ago by Duncan McGreggor (oubiwann) for use in systems and network monitoring software. It gained a small following with about 1300 total downloads, but was retired in 2008 in favor of netaddr.

Features that are present in NetCIDR but missing in netaddr are under discussion for potential inclusion. As missing features are added, they will be noted here.

Missing Features

  • A Networks object
  • The ability to build a collection of CIDRs that exactly span a start and end IP address
  • The ability to build a collection of CIDRs determined by IPs that are not to be included
  • The ability to build a collection of CIDRs that include only a given list of IPs
  • A utility for determining the traffic direction, given source IP, destination IP, and one or more CIDR objects representing one or more networks

API Differences

CIDR Representation and IPs

NetCIDR doesn't do a good job of distinguishing between IP addresses and CIDR representation objects. There are also inconsistencies in how IPs are represented. netaddr does a great job with this, and is completely consistent.

Examples

Host Range

Old and Busted - NetCIDR: ```

from netcidr import CIDR c = CIDR('172.16.4.0/27') c.getHostRange() (172.16.4.0, 172.16.4.31) ```

New Hotness - netaddr: ```

from netaddr import CIDR range = (c[0], c[-1]) (netaddr.address.IP('172.16.4.0'), netaddr.address.IP('172.16.4.31')) ```

Note that the netaddr results are IP objects. NetCIDR outputs ranges as a tuple of single-host CIDR objects. If you want to duplicate this output in netaddr, you can do the following: ```

c = CIDR('172.16.4.0/27') (c[0].cidr(), c[-1].cidr()) (netaddr.address.CIDR('172.16.4.0/32'), netaddr.address.CIDR('172.16.4.31/32')) ```

Host Count

Old and Busted - NetCIDR: ```

c = CIDR('192.168.0.64/26') c.getHostCount() 64 ```

New Hotness - netaddr: ```

c = CIDR('192.168.0.64/26') len(c) 64 ```

List of IPs

Old and Busted - NetCIDR: ```

c = CIDR('192.168.0.64/31') c.getIPs() ['192.168.0.64', '192.168.0.65'] ```

New Hotness - netaddr: ```

c = CIDR('192.168.0.64/31', klass=str) list(c) ['192.168.0.64', '192.168.0.65'] ```

Or, if you want to work with IP objects: ```

c = CIDR('192.168.0.64/31') list(c) [netaddr.address.IP('192.168.0.64'), netaddr.address.IP('192.168.0.65')] ```

In the first example above, note the use of the klass parameter. With that parameter, you can have netaddr produce string and other output formats: ```

c = CIDR('172.16.4.0/27', klass=str) (c[0], c[-1]) ('172.16.4.0', '172.16.4.31')

c = CIDR('172.16.4.0/27', klass=hex) (c[0], c[-1]) ('0xAC100400L', '0xAC10041FL')

c = CIDR('172.16.4.0/27', klass=int) (c[0], c[-1]) (2886730752L, 2886730783L) ```

IP Integer Tuple

Old and Busted - NetCIDR: ```

addr = CIDR('192.168.0.64') addr.getOctetTuple() (192, 168, 0, 64) ```

New Hotness - netaddr: ```

from netaddr import IP addr = IP('192.168.0.64') tuple(addr) (192, 168, 0, 64) ```

Comparisons

Old and Busted - NetCIDR: ```

c1 = CIDR('192.168.0.0')
c2 = CIDR('192.168.0.0') c1 > c2 False c1 < c2 False c1 == c2 True

c1 = CIDR('192.168.0.0') c2 = CIDR('192.168.0.1') c1 > c2 False c1 < c2 True c1 == c2 False

c1 = CIDR('192.168.117.1') c2 = CIDR('192.168.21.0') c3 = CIDR('192.168.4.1') c1 > c2 > c3 True c1 < c2 < c3 False c1 == c2 False ```

New Hotness - netaddr: ```

addr1 = IP('192.168.0.0') addr2 = IP('192.168.0.0') addr1 > addr2 False addr1 < addr2 False addr1 == addr2 True

addr1 = IP('192.168.0.0') addr2 = IP('192.168.0.1') addr1 > addr2 False addr1 < addr2 True addr1 == addr2 False

addr1 = IP('192.168.117.1') addr2 = IP('192.168.21.0') addr3 = IP('192.168.4.1')
addr1 > addr2 > addr3 True addr1 < addr2 < addr3 False addr1 == addr2 False ```

Containment

Old and Busted - NetCIDR: ```

from netcidr import Networks net = Networks([CIDR('192.168.3.0/24'), CIDR('10.0.1.0/29')]) CIDR('192.168.3.24') in net True CIDR('192.168.4.24') in net False CIDR('10.0.1.0') in net
True CIDR('10.0.1.7') in net True CIDR('10.0.1.8') in net False ```

New Hotness - netaddr:

As of right now, there is no CIDR collections object like netcidr.Networks. Neither is addition currently supported for netaddr.CIDR objects. For now, you can do the following: ```

IP('192.168.3.24') in CIDR('192.168.3.0/24') True IP('192.168.4.24') in CIDR('192.168.3.0/24') False IP('10.0.1.0') in CIDR('10.0.1.0/29') True IP('10.0.1.7') in CIDR('10.0.1.0/29') True IP('10.0.1.8') in CIDR('10.0.1.0/29') False ```