Skip to content

Instantly share code, notes, and snippets.

@wg1k
Created September 26, 2022 12:56
Show Gist options
  • Select an option

  • Save wg1k/161af4d7a78a38e167e7193c1b51cf41 to your computer and use it in GitHub Desktop.

Select an option

Save wg1k/161af4d7a78a38e167e7193c1b51cf41 to your computer and use it in GitHub Desktop.
wireguard - Exclude single ip
#!/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)))
@wg1k
Copy link
Author

wg1k commented Sep 26, 2022

Thanks to Daniel Lautenbacher

Slighly modified to read exclusion arguments from command line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment