=IP Matching Tests= Copyright (c) 2008-2012, David P. D. Moss. All rights reserved. {{{ >>> from netaddr import * >>> largest_matching_cidr('192.0.2.0', ['192.0.2.0']) IPNetwork('192.0.2.0/32') >>> largest_matching_cidr('192.0.2.0', ['10.0.0.1', '192.0.2.0']) IPNetwork('192.0.2.0/32') >>> largest_matching_cidr('192.0.2.0', ['10.0.0.1', '192.0.2.0', '224.0.0.1']) IPNetwork('192.0.2.0/32') >>> smallest_matching_cidr('192.0.2.0', ['10.0.0.1', '192.0.2.0', '224.0.0.1']) IPNetwork('192.0.2.0/32') >>> smallest_matching_cidr('192.0.2.0', ['10.0.0.1', '224.0.0.1']) >>> largest_matching_cidr('192.0.2.0', ['10.0.0.1', '224.0.0.1']) >>> networks = [str(c) for c in IPNetwork('192.0.2.128/27').supernet(22)] >>> networks ['192.0.0.0/22', '192.0.2.0/23', '192.0.2.0/24', '192.0.2.128/25', '192.0.2.128/26'] >>> all_matching_cidrs('192.0.2.0', networks) [IPNetwork('192.0.0.0/22'), IPNetwork('192.0.2.0/23'), IPNetwork('192.0.2.0/24')] >>> smallest_matching_cidr('192.0.2.0', networks) IPNetwork('192.0.2.0/24') >>> largest_matching_cidr('192.0.2.0', networks) IPNetwork('192.0.0.0/22') }}} Checking matches with varying IP address versions. {{{ >>> all_matching_cidrs('192.0.2.0', ['192.0.2.0/24']) [IPNetwork('192.0.2.0/24')] >>> all_matching_cidrs('192.0.2.0', ['::/96']) [] >>> all_matching_cidrs('::ffff:192.0.2.1', ['::ffff:192.0.2.0/96']) [IPNetwork('::ffff:192.0.2.0/96')] >>> all_matching_cidrs('::192.0.2.1', ['::192.0.2.0/96']) [IPNetwork('::192.0.2.0/96')] >>> all_matching_cidrs('::192.0.2.1', ['192.0.2.0/23']) [] >>> all_matching_cidrs('::192.0.2.1', ['192.0.2.0/24', '::192.0.2.0/120']) [IPNetwork('::192.0.2.0/120')] >>> all_matching_cidrs('::192.0.2.1', [IPNetwork('192.0.2.0/24'), IPNetwork('::192.0.2.0/120')]) [IPNetwork('::192.0.2.0/120')] }}}