Dns cname creation

Hi Community geeks,
I am very new to golang and I am trying to code an automation using go.
We are a set of admins, and we have a hosted zone in our DNS server. At times we need to create CNAME for user readability. I have valid credentials for this job to be done manually. When trying the go to automate this process, its just not working.

package main

import (
	"fmt"
	"github.com/miekg/dns"
	// "golang.org/x/net/context"
	"net"
	//"crypto/tls"
	//"golang.org/x/net/idna"
)

func main() {

	dnsServer := "subdomain.domain.com"
	dnsUsername := "111111-adm"
	dnsPassword := "*******"


	domain := "subdomain.domain.com"
	cnameName := "prettydns-demo.subdomain.domain.com"
	cnameData := "actual-cname.subdomain.domain.com"

	msg := dns.Msg{}
	msg.SetUpdate(domain)

	cnameRecord := &dns.CNAME{
		Hdr: dns.RR_Header{
			Name:   dns.Fqdn(cnameName),
			Rrtype: dns.TypeCNAME,
			Class:  dns.ClassINET,
			Ttl:    300,
		},
		Target: cnameData,
	}

        msg.Insert([]dns.RR{cnameRecord})

	serverAddr := dnsServer + ":53"

        // Open a TCP connection to the DNS server
	conn, err := net.Dial("tcp", serverAddr)
	if err != nil {
		fmt.Printf("Error opening TCP connection: %v\n", err)
		return
	}

	defer conn.Close()

	// Send the update message to the DNS server
	_, err = conn.Write([]byte(fmt.Sprintf("%s:%s", dnsUsername, dnsPassword)))
	if err != nil {
		fmt.Printf("Error sending update request: %v\n", err)
		return
	}

	// Read the response from the DNS server Over tcp
	responseBytes := make([]byte, 1024)
	n, err := conn.Read(responseBytes)
	if err != nil {
		fmt.Printf("Error reading response: %v\n", err)
		return
	}

	response, err := client.ReadMsg(conn)
	if err != nil {
		fmt.Printf("Error reading response: %v\n", err)
		return
	}
	if response != nil && response.Rcode == dns.RcodeSuccess {
		fmt.Println("CNAME record created successfully!")
	} else {
		fmt.Println("Error creating CNAME record.")
	}
}

I don’t have prior experience with go, so if I ask any stupid question I apologize for that. I just have tried many things from googling, but its just not working. Any help on this would be a great learning for me.

Regards,
Saha

Dos your code compile ? I got two errors:

n, err := conn.Read(responseBytes)

n is not used anf

response, err := client.ReadMsg(conn)

client is undefined…

Thanks for the answer. However, even if I try remediating all these I still don’t get to do this job. Not sure for authentication what do I have to do.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.