Skip to content

Instantly share code, notes, and snippets.

@ictus4u
Created September 26, 2022 12:56
Show Gist options
  • Save ictus4u/161af4d7a78a38e167e7193c1b51cf41 to your computer and use it in GitHub Desktop.
Save ictus4u/161af4d7a78a38e167e7193c1b51cf41 to your computer and use it in GitHub Desktop.

Revisions

  1. ictus4u created this gist Sep 26, 2022.
    19 changes: 19 additions & 0 deletions subnets.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    #!/usr/bin/env python
    from ipaddress import ip_network
    from sys import argv

    start = '0.0.0.0/0'
    exclude = argv[1:]

    result = [ip_network(start)]
    for x in exclude:
    n = ip_network(x)
    new = []
    for y in result:
    if y.overlaps(n):
    new.extend(y.address_exclude(n))
    else:
    new.append(y)
    result = new

    print(','.join(str(x) for x in sorted(result)))