diff options
author | makefu <github@syntax-fehler.de> | 2017-12-18 21:24:28 +0100 |
---|---|---|
committer | makefu <github@syntax-fehler.de> | 2017-12-18 21:24:28 +0100 |
commit | af308642031f971bc30c5694385eb7af1e9ea618 (patch) | |
tree | 7e9e55f7d5de874a61a5d9294065a4f9749ee83e /krebs/5pkgs/simple/cidr2glob.nix | |
parent | 4feafd70204f9c13500bd427d250fac60ca595ef (diff) | |
parent | 676c76dd8e0b5cbe3d1bdba49b21b1b5cfc578a1 (diff) |
Merge branch 'master' of prism:stockholm
Diffstat (limited to 'krebs/5pkgs/simple/cidr2glob.nix')
-rw-r--r-- | krebs/5pkgs/simple/cidr2glob.nix | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/krebs/5pkgs/simple/cidr2glob.nix b/krebs/5pkgs/simple/cidr2glob.nix new file mode 100644 index 000000000..9b0b3f86b --- /dev/null +++ b/krebs/5pkgs/simple/cidr2glob.nix @@ -0,0 +1,30 @@ +{ python, writeScriptBin, ... }: + +let + pythonEnv = python.withPackages (ps: [ ps.netaddr ]); +in + writeScriptBin "cidr2glob" '' + #! ${pythonEnv}/bin/python + + import netaddr + import re + import sys + + def cidr2glob(cidr): + net = netaddr.IPNetwork(cidr) + + if net.prefixlen <= 8: + return map(lambda subnet: re.sub(r'\.0\.0\.0$', '.*', str(subnet.ip)), net.subnet(8)) + elif net.prefixlen <= 16: + return map(lambda subnet: re.sub(r'\.0\.0$', '.*', str(subnet.ip)), net.subnet(16)) + elif net.prefixlen <= 24: + return map(lambda subnet: re.sub(r'\.0$', '.*', str(subnet.ip)), net.subnet(24)) + else: + return map(lambda ip: str(ip), list(net)) + + if __name__ == "__main__": + for cidr in sys.stdin: + for glob in cidr2glob(cidr): + print glob + + '' |