summaryrefslogtreecommitdiffstats
path: root/krebs/5pkgs/simple/cidr2glob.nix
blob: 9b0b3f86b194e45001fa0a96d8ed826ab25e46ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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

  ''