Skip to content

Instantly share code, notes, and snippets.

@voukpaw
Forked from pklaus/rdns.py
Created October 12, 2017 11:23
Show Gist options
  • Select an option

  • Save voukpaw/429d4337e4d1aed6e0c12d0720bd976e to your computer and use it in GitHub Desktop.

Select an option

Save voukpaw/429d4337e4d1aed6e0c12d0720bd976e to your computer and use it in GitHub Desktop.

Revisions

  1. @pklaus pklaus revised this gist Aug 25, 2014. 1 changed file with 19 additions and 26 deletions.
    45 changes: 19 additions & 26 deletions rdns.py
    Original file line number Diff line number Diff line change
    @@ -12,45 +12,38 @@
    import sys

    try:
    import ipaddress
    from ipaddress import IPv6Address, IPv6Network
    except ImportError:
    sys.stderr.write("This script needs the ipaddress module from Python3.3+. "
    "If you run an older version of Python, you should also run an older version of this script.\n")
    sys.exit(2)

    class IPv6Network(ipaddress.IPv6Network):
    # Monkey patching ipaddress.IPv6Network with a custom method
    def combine_with(self, other):
    """
    Monkey patched IPv6Network which can combine an IPv6 Network with a
    host specifier in the form of an IPv6 address.
    Function to monkey patch IPv6Network:
    Can combine an IPv6 Network with a host specifier in the form of an IPv6 address.
    n1 = IPv6Network('2003::/64')
    d1 = ipaddress.IPv6Address('::1')
    print(n1.combine_with(d1))
    """
    def combine_with(self, other):
    """
    Can be combined with a device specifier
    (expressed as an IPv6Address).
    """
    first = self[0]

    if ((int('1' * self.prefixlen, 2) << (128-self.prefixlen) ) & other._ip) > 0:
    raise ValueError("Cannot combine an IPv6 address with this network. Some bits overlap.")
    return IPv6Address(first._ip | other._ip)

    class IPv6Address(ipaddress.IPv6Address):
    first = self[0]
    if ((int('1' * self.prefixlen, 2) << (128-self.prefixlen) ) & other._ip) > 0:
    raise ValueError("Cannot combine an IPv6 address with this network. Some bits overlap.")
    return IPv6Address(first._ip | other._ip)
    setattr(IPv6Network, 'combine_with', combine_with)

    # Monkey patching ipaddress.IPv6Address with reverse_pointer from Python 3.5+.
    def reverse_pointer(self):
    """
    Monkey patched IPv6Address which includes the @property reverse_pointer
    that is included with Python 3.5+.
    Return the reverse DNS pointer name for the IPv6 address.
    taken from http://hg.python.org/cpython/file/default/Lib/ipaddress.py
    """
    @property
    def reverse_pointer(self):
    """
    Return the reverse DNS pointer name for the IPv6 address.
    taken from http://hg.python.org/cpython/file/default/Lib/ipaddress.py
    """
    reverse_chars = self.exploded[::-1].replace(':', '')
    return '.'.join(reverse_chars) + '.ip6.arpa'
    reverse_chars = self.exploded[::-1].replace(':', '')
    return '.'.join(reverse_chars) + '.ip6.arpa'
    setattr(IPv6Address, 'reverse_pointer', property(reverse_pointer))


    ## configure your RDNS generation here:
    host = "example.com."
  2. @pklaus pklaus revised this gist Aug 24, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rdns.py
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@
    import ipaddress
    except ImportError:
    sys.stderr.write("This script needs the ipaddress module from Python3.3+. "
    "If you run an older Python version, get an older version of this script.\n")
    "If you run an older version of Python, you should also run an older version of this script.\n")
    sys.exit(2)

    class IPv6Network(ipaddress.IPv6Network):
  3. @pklaus pklaus revised this gist Aug 24, 2014. 1 changed file with 16 additions and 3 deletions.
    19 changes: 16 additions & 3 deletions rdns.py
    Original file line number Diff line number Diff line change
    @@ -13,9 +13,8 @@

    try:
    import ipaddress
    from ipaddress import IPv6Address
    except ImportError:
    sys.stderr.write("This script needs the ipaddress module from Python3.5+. "
    sys.stderr.write("This script needs the ipaddress module from Python3.3+. "
    "If you run an older Python version, get an older version of this script.\n")
    sys.exit(2)

    @@ -37,7 +36,21 @@ def combine_with(self, other):

    if ((int('1' * self.prefixlen, 2) << (128-self.prefixlen) ) & other._ip) > 0:
    raise ValueError("Cannot combine an IPv6 address with this network. Some bits overlap.")
    return ipaddress.IPv6Address(first._ip | other._ip)
    return IPv6Address(first._ip | other._ip)

    class IPv6Address(ipaddress.IPv6Address):
    """
    Monkey patched IPv6Address which includes the @property reverse_pointer
    that is included with Python 3.5+.
    """
    @property
    def reverse_pointer(self):
    """
    Return the reverse DNS pointer name for the IPv6 address.
    taken from http://hg.python.org/cpython/file/default/Lib/ipaddress.py
    """
    reverse_chars = self.exploded[::-1].replace(':', '')
    return '.'.join(reverse_chars) + '.ip6.arpa'

    ## configure your RDNS generation here:
    host = "example.com."
  4. @pklaus pklaus revised this gist Aug 22, 2014. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions rdns.py
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,13 @@
    #!/usr/bin/env python

    """ This is a Python script that helps you create reverse DNS zone files for the Bind Name Server.
    I published it in my blog post http://goo.gl/CJwly ."""
    """
    rdns.py
    This is a Python script that helps you create
    reverse DNS zone files for the Bind Name Server.
    I published it together with this blog post: http://goo.gl/CJwly .
    """

    import sys

  5. @pklaus pklaus revised this gist Aug 22, 2014. 1 changed file with 38 additions and 17 deletions.
    55 changes: 38 additions & 17 deletions rdns.py
    Original file line number Diff line number Diff line change
    @@ -3,24 +3,45 @@
    """ This is a Python script that helps you create reverse DNS zone files for the Bind Name Server.
    I published it in my blog post http://goo.gl/CJwly ."""

    ## This script needs IPy.py:
    ## wget https://raw.github.com/haypo/python-ipy/master/IPy.py --no-check-certificate
    from IPy import IP
    class cIP (IP):
    def __or__(self, other):
    if self.ip & other.ip >0:
    raise ValueError("Bitwise or is not defined for overlapping networks & identifiers.")
    return cIP(self.ip | other.ip)
    import sys

    try:
    import ipaddress
    from ipaddress import IPv6Address
    except ImportError:
    sys.stderr.write("This script needs the ipaddress module from Python3.5+. "
    "If you run an older Python version, get an older version of this script.\n")
    sys.exit(2)

    class IPv6Network(ipaddress.IPv6Network):
    """
    Monkey patched IPv6Network which can combine an IPv6 Network with a
    host specifier in the form of an IPv6 address.
    n1 = IPv6Network('2003::/64')
    d1 = ipaddress.IPv6Address('::1')
    print(n1.combine_with(d1))
    """
    def combine_with(self, other):
    """
    Can be combined with a device specifier
    (expressed as an IPv6Address).
    """
    first = self[0]

    if ((int('1' * self.prefixlen, 2) << (128-self.prefixlen) ) & other._ip) > 0:
    raise ValueError("Cannot combine an IPv6 address with this network. Some bits overlap.")
    return ipaddress.IPv6Address(first._ip | other._ip)

    ## configure your RDNS generation here:
    host = "example.com."
    first_name_server = "ns1.example.com."
    administrative_contact = "admin.example.com."
    subnet = cIP("2001:db8::/32")
    subnet = IPv6Network("2001:db8::/32")
    rdns_entries = []
    # a list of RDNS entries which are of the form (IPv6 Address , Fully Qualified Domain Name)
    rdns_entries.append( ( subnet | cIP("::1"), "host1."+host ) )
    rdns_entries.append( ( subnet | cIP("::2"), "host2."+host ) )
    # a list of RDNS entries which are of the form (IPv6Address, FQDN)
    rdns_entries.append((subnet.combine_with(IPv6Address("::1")), "host1."+host))
    rdns_entries.append((subnet.combine_with(IPv6Address("::2")), "host2."+host))

    ## should not need to modify:
    record_ttl = "1h"
    @@ -40,15 +61,15 @@ def __or__(self, other):

    print("@ IN SOA %s %s (" % (first_name_server, administrative_contact) )
    print(" %s ; serial" % zone_serial)
    print(" %s ; slave refresh interval" % slave_refresh_interval)
    print(" %s ; slave retry interval" % slave_retry_interval)
    print(" %s ; slave copy expire time" % slave_expiration_time)
    print(" %s ; NXDOMAIN cache time" % nxdomain_cache_time)
    print(" %s ; slave refresh interval" % slave_refresh_interval)
    print(" %s ; slave retry interval" % slave_retry_interval)
    print(" %s ; slave copy expire time" % slave_expiration_time)
    print(" %s ; NXDOMAIN cache time" % nxdomain_cache_time)
    print(" )")

    print("; domain name servers")
    print("@ IN NS %s" % first_name_server)

    print("; IPv6 PTR entries")
    for rdns_entry in rdns_entries:
    print("%s IN PTR %s" % (rdns_entry[0].reverseNames()[0], rdns_entry[1]) )
    print("%s IN PTR %s" % (rdns_entry[0].reverse_pointer, rdns_entry[1]) )
  6. @pklaus pklaus revised this gist Mar 27, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rdns.py
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    class cIP (IP):
    def __or__(self, other):
    if self.ip & other.ip >0:
    raise ValueError("Bitwise and is not defined for overlapping networks & identifiers.")
    raise ValueError("Bitwise or is not defined for overlapping networks & identifiers.")
    return cIP(self.ip | other.ip)

    ## configure your RDNS generation here:
  7. @pklaus pklaus revised this gist Mar 11, 2012. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions rdns.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    #!/usr/bin/env python

    """ This is a Python script that helps you create reverse DNS zone files for the Bind Name Server. """
    """ This is a Python script that helps you create reverse DNS zone files for the Bind Name Server.
    I published it in my blog post http://goo.gl/CJwly ."""

    ## This script needs IPy.py:
    ## wget https://raw.github.com/haypo/python-ipy/master/IPy.py --no-check-certificate
    @@ -32,8 +33,8 @@ def __or__(self, other):

    ### Begin of the output generation

    print("; Zone file built with the Python Tool")
    print("; blog.philippklaus.de")
    print("; Zone file built with the Python Tool rdns.py:")
    print("; " + __doc__.replace("\n","\n; ") )

    print("$TTL %s ; Default TTL" % record_ttl )

  8. @pklaus pklaus created this gist Mar 11, 2012.
    53 changes: 53 additions & 0 deletions rdns.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    #!/usr/bin/env python

    """ This is a Python script that helps you create reverse DNS zone files for the Bind Name Server. """

    ## This script needs IPy.py:
    ## wget https://raw.github.com/haypo/python-ipy/master/IPy.py --no-check-certificate
    from IPy import IP
    class cIP (IP):
    def __or__(self, other):
    if self.ip & other.ip >0:
    raise ValueError("Bitwise and is not defined for overlapping networks & identifiers.")
    return cIP(self.ip | other.ip)

    ## configure your RDNS generation here:
    host = "example.com."
    first_name_server = "ns1.example.com."
    administrative_contact = "admin.example.com."
    subnet = cIP("2001:db8::/32")
    rdns_entries = []
    # a list of RDNS entries which are of the form (IPv6 Address , Fully Qualified Domain Name)
    rdns_entries.append( ( subnet | cIP("::1"), "host1."+host ) )
    rdns_entries.append( ( subnet | cIP("::2"), "host2."+host ) )

    ## should not need to modify:
    record_ttl = "1h"
    from datetime import datetime
    zone_serial = datetime.now().strftime("%Y%m%d%H%M%S")
    slave_refresh_interval = "1h"
    slave_retry_interval = "15m"
    slave_expiration_time = "1w"
    nxdomain_cache_time = "1h"

    ### Begin of the output generation

    print("; Zone file built with the Python Tool")
    print("; blog.philippklaus.de")

    print("$TTL %s ; Default TTL" % record_ttl )

    print("@ IN SOA %s %s (" % (first_name_server, administrative_contact) )
    print(" %s ; serial" % zone_serial)
    print(" %s ; slave refresh interval" % slave_refresh_interval)
    print(" %s ; slave retry interval" % slave_retry_interval)
    print(" %s ; slave copy expire time" % slave_expiration_time)
    print(" %s ; NXDOMAIN cache time" % nxdomain_cache_time)
    print(" )")

    print("; domain name servers")
    print("@ IN NS %s" % first_name_server)

    print("; IPv6 PTR entries")
    for rdns_entry in rdns_entries:
    print("%s IN PTR %s" % (rdns_entry[0].reverseNames()[0], rdns_entry[1]) )