summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2017-12-12 21:06:21 +0100
committertv <tv@krebsco.de>2017-12-12 21:06:21 +0100
commit2a3a3248def505c64c3f596acefa894959d4a20d (patch)
tree53f0b92b163c8a308ca7f526aa929eef1730b120
parentfa3c5eb1d83d2948d9810e1fd9e3d6ef53830722 (diff)
cidr2glob: init
Based on https://gist.github.com/speshak/b62fa28b49377cda8047cb227837244c
-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
+
+ ''