summaryrefslogtreecommitdiffstats
path: root/krebs/5pkgs/simple/cidr2glob.nix
diff options
context:
space:
mode:
authornin <nineinchnade@gmail.com>2017-12-14 21:13:46 +0100
committernin <nineinchnade@gmail.com>2017-12-14 21:13:46 +0100
commit37771ad34e18e0657636c8d0eb5b7392e3b89ba6 (patch)
tree8abda683b425494d2a273fc42d7573499b97a657 /krebs/5pkgs/simple/cidr2glob.nix
parent7ed6fd18bb99884889a76ad9f597193861f44dc9 (diff)
parent04f7ae22d6d0720d06f78c712eb9cd245cefce82 (diff)
Merge remote-tracking branch 'prism/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
+
+ ''