Skip to content

Instantly share code, notes, and snippets.

@wilhelm-murdoch
Last active October 22, 2021 13:08
Show Gist options
  • Select an option

  • Save wilhelm-murdoch/3e6af107f378ada36e91601aedc636d1 to your computer and use it in GitHub Desktop.

Select an option

Save wilhelm-murdoch/3e6af107f378ada36e91601aedc636d1 to your computer and use it in GitHub Desktop.

Revisions

  1. wilhelm-murdoch revised this gist Feb 14, 2019. No changes.
  2. wilhelm-murdoch created this gist Feb 14, 2019.
    75 changes: 75 additions & 0 deletions cidr.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    #!/usr/bin/env python

    import ipaddr

    accounts = {
    'flood-beta': {
    'public': [
    '10.1.32.0/20',
    '10.1.96.0/20',
    '10.1.160.0/20'
    ],
    'private': [
    '10.1.0.0/19',
    '10.1.64.0/19',
    '10.1.128.0/19'
    ],
    'spare': [
    '10.1.48.0/20',
    '10.1.112.0/20',
    '10.1.176.0/20'
    ]
    },
    'flood-prod': {
    'public': [
    '10.2.32.0/20',
    '10.2.96.0/20',
    '10.2.160.0/20'
    ],
    'private': [
    '10.2.0.0/19',
    '10.2.64.0/19',
    '10.2.128.0/19'
    ],
    'spare': [
    '10.2.48.0/20',
    '10.2.112.0/20',
    '10.2.176.0/20'
    ]
    },
    'flood-master': {
    'public': [
    '10.0.1.0/24',
    '10.0.2.0/24',
    '10.0.3.0/24'
    ],
    'private': [
    '10.0.0.0/24',
    '10.0.4.0/24'
    ],
    'spare': []
    }
    }


    for account, blocks in accounts.iteritems():
    print account
    print '=========='
    for block, cidrs in blocks.iteritems():
    print block + ':'
    overlapped = False
    for cidr in cidrs:
    n1 = ipaddr.IPNetwork(cidr)
    print '===> ' + cidr
    for account2, blocks2 in accounts.iteritems():
    if account != account2:
    for block2, cidrs2 in blocks2.iteritems():
    for cidr2 in cidrs2:
    n2 = ipaddr.IPNetwork(cidr2)
    if n1.overlaps(n2):
    overlapped = True
    print '---> overlaps ' + cidr2 + ' in ' + account2
    if not overlapped:
    print 'all clear'
    print
    print