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