summaryrefslogtreecommitdiffstats
path: root/krebs/5pkgs/simple/cidr2glob.nix
diff options
context:
space:
mode:
authorlassulus <lassulus@lassul.us>2017-12-12 21:56:24 +0100
committerlassulus <lassulus@lassul.us>2017-12-12 21:56:24 +0100
commit29138305788cb2229b9d8e6c329c743c2d8a6711 (patch)
treeb472612cd5051ccff6fe01a692353e3078785b70 /krebs/5pkgs/simple/cidr2glob.nix
parent47f3d044e4d8e45168d54dc69368a598330b76ae (diff)
parent19fcba24f1ef050a8f8d553f09348adb2a007041 (diff)
Merge remote-tracking branch 'ni/master'
Diffstat (limited to 'krebs/5pkgs/simple/cidr2glob.nix')
-rw-r--r--krebs/5pkgs/simple/cidr2glob.nix30
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
+
+ ''