[Python] Can’t create CNAME DNS record using ldap3 and python

Discussão em 'Python' iniciado por Stack, Julho 14, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to create a CNAME DNS record using Python and ldap3. I don’t get any error but it doesn’t create the record in the AD DNS.

    The code I tried:

    cname_record = “mycname”
    a_record = “example”

    server = Server(ldap_server, get_info=ALL)

    parts = a_record.split(‘.’)
    data_bytes = b’’
    for part in parts:
    if part:
    data_bytes += len(part).to_bytes(1, ‘big’) + part.encode(“ascii”)

    data_bytes += b’\x00’

    data_length = struct.pack(“H”, len(data_bytes))
    dnsRecordType = struct.pack(“H”, 5)

    version = struct.pack(“B”, 5)
    rank = struct.pack(“B”, 240)
    flags = struct.pack(“H”, 0)
    serial = struct.pack(“L”, 4)
    ttl = struct.pack(“!L”, 3600)
    reserved = struct.pack(“L”, 0)
    ts = struct.pack(“L”, 0)
    a_rec = data_bytes


    dns_record_data = data_length + dnsRecordType + version + rank + flags + serial + ttl + reserved + ts + ip

    dns_zone = “DC=tony.com,CN=MicrosoftDNS,DC=DomainDnsZones,DC=tony,DC=com”
    record_dn = f”DC={cname_record},{dns_zone}”

    with Connection(server, user = “DOMAIN\\MYUSER”, password = “PASS”, auto_bind = True) as conn:
    conn.add(dn=record_dn, object_class=[“dnsNode”], attributes={“dnsRecord”: [dns_record_data]})
    print(conn.result)

    Continue reading...

Compartilhe esta Página