summaryrefslogtreecommitdiffstats
path: root/krebs/5pkgs/simple/cidr2glob.nix
blob: 47a75ea411365bb2025669d514d0cd2861e173ce (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
31
{ python3, writeScriptBin, ... }:

let
  python = python3;
  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)

  ''