mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-27 10:58:37 -06:00
Modify reverse bind export
output the whole zone if prefix is smaller than the zone
This commit is contained in:
parent
7df6353296
commit
2478b354d1
@ -13,6 +13,7 @@ import dns.models
|
|||||||
from .fields import IPNetworkField, IPAddressField
|
from .fields import IPNetworkField, IPAddressField
|
||||||
|
|
||||||
import time, ipaddress
|
import time, ipaddress
|
||||||
|
import netaddr
|
||||||
|
|
||||||
|
|
||||||
AF_CHOICES = (
|
AF_CHOICES = (
|
||||||
@ -349,28 +350,34 @@ class Prefix(CreatedUpdatedModel):
|
|||||||
pslash = int(str(self.prefix).split('/')[1])
|
pslash = int(str(self.prefix).split('/')[1])
|
||||||
|
|
||||||
if pslash > 16:
|
if pslash > 16:
|
||||||
# create /24 zones
|
zslash = 24
|
||||||
ztype = 24
|
|
||||||
else:
|
else:
|
||||||
# create /16 zones
|
zslash = 16
|
||||||
ztype = 16
|
|
||||||
|
|
||||||
|
if pslash > zslash:
|
||||||
|
pslash = zslash
|
||||||
|
|
||||||
|
p = IPNetwork(unicode('.'.join(pbytes)+'/'+str(pslash)))
|
||||||
|
|
||||||
|
ipaddresses = IPAddress.objects.filter(family=4)
|
||||||
for ip in ipaddresses:
|
for ip in ipaddresses:
|
||||||
ibytes = str(ip.address).split('/')[0].split('.')
|
ibytes = str(ip.address).split('/')[0].split('.')
|
||||||
islash = str(ip.address).split('/')[1]
|
islash = str(ip.address).split('/')[1]
|
||||||
|
i = netaddr.IPAddress(unicode('.'.join(ibytes)))
|
||||||
|
|
||||||
if ztype == 24:
|
if i in p:
|
||||||
zone_id = ibytes[2]+'.'+ibytes[1]+'.'+ibytes[0]+'.in-addr.arpa.'
|
if zslash == 24:
|
||||||
if not zone_id in zones:
|
zone_id = ibytes[2] + '.' + ibytes[1] + '.' + ibytes[0] + '.in-addr.arpa.'
|
||||||
zones[zone_id] = header(zone_id)
|
if not zone_id in zones:
|
||||||
zones[zone_id] += ibytes[3].ljust(3) + ' IN PTR ' + ip.hostname.ljust(40) + ' ; ' + ip.description.ljust(20) + ' ; gen by netbox ( '+time.strftime('%A %B %d %Y %H:%M:%S',time.localtime())+' ) \n'
|
zones[zone_id] = header(zone_id)
|
||||||
|
zones[zone_id] += ibytes[3].ljust(3) + ' IN PTR ' + ip.hostname.ljust(40) + ' ; ' + ip.description.ljust(20) + ' ; gen by netbox ( '+time.strftime('%A %B %d %Y %H:%M:%S',time.localtime())+' ) \n'
|
||||||
|
else:
|
||||||
|
zone_id = ibytes[1]+'.'+ibytes[0]+'.in-addr.arpa.'
|
||||||
|
if not zone_id in zones:
|
||||||
|
zones[zone_id] = header(zone_id)
|
||||||
|
zones[zone_id] += (ibytes[3]+'.'+ibytes[2]).ljust(7) + ' IN PTR ' + ip.hostname.ljust(40) + ' ; ' + ip.description.ljust(20) + ' ; gen by netbox ( '+time.strftime('%A %B %d %Y %H:%M:%S',time.localtime())+' ) \n'
|
||||||
|
|
||||||
|
|
||||||
else:
|
|
||||||
zone_id = ibytes[1]+'.'+ibytes[0]+'.in-addr.arpa.'
|
|
||||||
if not zone_id in zones:
|
|
||||||
zones[zone_id] = header(zone_id)
|
|
||||||
zones[zone_id] += (ibytes[3]+'.'+ibytes[2]).ljust(7) + ' IN PTR ' + ip.hostname.ljust(40) + ' ; ' + ip.description.ljust(20) + ' ; gen by netbox ( '+time.strftime('%A %B %d %Y %H:%M:%S',time.localtime())+' ) \n'
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
pfull = str(ipaddress.IPv6Address(unicode(str(self.prefix).split('/')[0])).exploded)
|
pfull = str(ipaddress.IPv6Address(unicode(str(self.prefix).split('/')[0])).exploded)
|
||||||
@ -378,20 +385,29 @@ class Prefix(CreatedUpdatedModel):
|
|||||||
pdigits = pfull.replace(':','')
|
pdigits = pfull.replace(':','')
|
||||||
pslash = int(str(self.prefix).split('/')[1])
|
pslash = int(str(self.prefix).split('/')[1])
|
||||||
|
|
||||||
ztype = pslash if pslash % 16 == 0 else pslash/16+16
|
zslash = pslash if pslash % 16 == 0 else pslash/16+16
|
||||||
|
if pslash > zslash:
|
||||||
|
pslash = zslash
|
||||||
|
|
||||||
|
p = IPNetwork(unicode(pfull+'/'+str(pslash)))
|
||||||
|
|
||||||
|
ipaddresses = IPAddress.objects.filter(family=6)
|
||||||
for ip in ipaddresses:
|
for ip in ipaddresses:
|
||||||
ifull = str(ipaddress.IPv6Address(unicode(str(ip.address).split('/')[0])).exploded)
|
ifull = str(ipaddress.IPv6Address(unicode(str(ip.address).split('/')[0])).exploded)
|
||||||
inibbles = ifull.split(':')
|
inibbles = ifull.split(':')
|
||||||
idigits = ifull.replace(':','')[::-1]
|
idigits = ifull.replace(':','')[::-1]
|
||||||
islash = int(str(ip.address).split('/')[1])
|
islash = int(str(ip.address).split('/')[1])
|
||||||
|
|
||||||
pdigitszone = pdigits[:ztype/4][::-1]
|
i = netaddr.IPAddress(unicode(ifull))
|
||||||
zone_id = '.'.join(pdigitszone)+'.ip6.arpa.'
|
|
||||||
if not zone_id in zones:
|
|
||||||
zones[zone_id] = header(zone_id)
|
|
||||||
|
|
||||||
zones[zone_id] += ('.'.join(idigits[:32-ztype/4])).ljust(30)+' IN PTR ' + ip.hostname.ljust(40) + ' ; ' + ip.description.ljust(20) + ' ; gen by netbox ( '+time.strftime('%A %B %d %Y %H:%M:%S',time.localtime())+' ) \n'
|
if i in p:
|
||||||
|
|
||||||
|
pdigitszone = pdigits[:pslash/4][::-1]
|
||||||
|
zone_id = '.'.join(pdigitszone)+'.ip6.arpa.'
|
||||||
|
if not zone_id in zones:
|
||||||
|
zones[zone_id] = header(zone_id)
|
||||||
|
|
||||||
|
zones[zone_id] += ('.'.join(idigits[:32-pslash/4])).ljust(30)+' IN PTR ' + ip.hostname.ljust(40) + ' ; ' + ip.description.ljust(20) + ' ; gen by netbox ( '+time.strftime('%A %B %d %Y %H:%M:%S',time.localtime())+' ) \n'
|
||||||
|
|
||||||
|
|
||||||
for z in zones:
|
for z in zones:
|
||||||
|
Loading…
Reference in New Issue
Block a user