Created
January 7, 2019 15:33
-
-
Save johanot/7646d199950073d8f02e05893bacadb9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| reservations = | |
| let | |
| program = pkgs.writeText "cidr-filter.py" '' | |
| import ipaddress | |
| import json | |
| import sys | |
| with open(sys.argv[1]) as f: | |
| data = json.load(f) | |
| net = ipaddress.ip_network(sys.argv[2]) | |
| out = [] | |
| for val in data: | |
| host = ipaddress.ip_network(val["ip"]) | |
| if net.overlaps(host): | |
| out.append(val) | |
| print(json.dumps(out)) | |
| ''; | |
| filterCidr = with pkgs; runCommand "cidr-filter" { buildInputs = [ python3 python3Packages.ipaddress ]; } '' | |
| mkdir -p $out | |
| python ${toString program} "${writeText "input.json" (builtins.toJSON (lib.attrValues reservations))}" "${subnet}" >$out/out.json | |
| ''; | |
| in | |
| with builtins; map hostToKea (fromJSON (readFile "${filterCidr}/out.json")); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment