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 (
	// "golang.org/x/net/context"

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{}

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


	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)

	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)

	// 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)

	response, err := client.ReadMsg(conn)
	if err != nil {
		fmt.Printf("Error reading response: %v\n", err)
	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.


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.