2022-06-28 07:50:47 +00:00
|
|
|
{ python3, writeScriptBin, ... }:
|
2017-12-12 20:06:21 +00:00
|
|
|
|
|
|
|
let
|
2022-06-28 07:50:47 +00:00
|
|
|
python = python3;
|
2017-12-12 20:06:21 +00:00
|
|
|
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):
|
2022-06-28 07:50:47 +00:00
|
|
|
print(glob)
|
2017-12-12 20:06:21 +00:00
|
|
|
|
|
|
|
''
|