-
-
Save tomislacker/e6cb0e6396701d4b8343420fd801aceb to your computer and use it in GitHub Desktop.
Convert CIDR networks into string glob (like what ssh_config expects)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
from netaddr import * | |
import re | |
import pprint | |
def cidr2glob(cidr): | |
network = IPNetwork(cidr) | |
if network.prefixlen <= 16: | |
return map(lambda subnet: re.sub(r'\.0\.0$', '.*', str(subnet.ip)), network.subnet(16)) | |
elif network.prefixlen <= 24: | |
return map(lambda subnet: re.sub(r'\.0$', '.*', str(subnet.ip)), network.subnet(24)) | |
else: | |
return map(lambda ip: str(ip), list(network)) | |
for net in ['10.255.0.0/11', '10.255.102.0/22', '10.255.103.0/24', '10.255.102.2/28']: | |
print net | |
pprint.pprint(cidr2glob(net)) | |
print "\n\n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10.255.0.0/11 | |
['10.224.*', | |
'10.225.*', | |
'10.226.*', | |
'10.227.*', | |
'10.228.*', | |
'10.229.*', | |
'10.230.*', | |
'10.231.*', | |
'10.232.*', | |
'10.233.*', | |
'10.234.*', | |
'10.235.*', | |
'10.236.*', | |
'10.237.*', | |
'10.238.*', | |
'10.239.*', | |
'10.240.*', | |
'10.241.*', | |
'10.242.*', | |
'10.243.*', | |
'10.244.*', | |
'10.245.*', | |
'10.246.*', | |
'10.247.*', | |
'10.248.*', | |
'10.249.*', | |
'10.250.*', | |
'10.251.*', | |
'10.252.*', | |
'10.253.*', | |
'10.254.*', | |
'10.255.*'] | |
10.255.102.0/22 | |
['10.255.100.*', '10.255.101.*', '10.255.102.*', '10.255.103.*'] | |
10.255.103.0/24 | |
['10.255.103.*'] | |
10.255.102.2/28 | |
['10.255.102.0', | |
'10.255.102.1', | |
'10.255.102.2', | |
'10.255.102.3', | |
'10.255.102.4', | |
'10.255.102.5', | |
'10.255.102.6', | |
'10.255.102.7', | |
'10.255.102.8', | |
'10.255.102.9', | |
'10.255.102.10', | |
'10.255.102.11', | |
'10.255.102.12', | |
'10.255.102.13', | |
'10.255.102.14', | |
'10.255.102.15'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment