Testing DNS resolution with Go

Ovidiu Borlean
2 min readNov 28, 2022

The scope of this article is to provide a simple solution to measure DNS resolution and latency in a continuous loopback with logging capabilities.

The script is written in Golang, it can be run with the following command, or it can be compiled on the platform when needs to run

# Running standalone script
go run dnstest.go

# Building platform specific binary
go build dnstest.go

The script is accepting command line arguments for the hostnamea and delay between tests. It can be run as follows

dnstest 2 <hostname1> <hostname2> <hostname3> ...
# Example: dnstes 2 google.com microsoft.com udemy.com

It will display the A records of the domain name, IP address associated with that domain, specific resolve time for that hostname and also global resolve time for all the provided domains. For any failure in DNS resolution, it will write the failed domain name along with the timestamp in a local file dnsreq.log.

package main

import (
"net"
"fmt"
"os"
"time"
"strings"
"strconv"
)

var host string = ""

func lookUp(host string) bool {
start := time.Now()
ips, err := net.LookupIP(host)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not get IPs: %v\n", err)
//os.Exit(1)
f, err := os.OpenFile("dnsreq.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
}
defer f.Close()
timeString := start.Format("2006-01-02 15:04:05")
str := []string{timeString, " ", host, "\n"}
if _, err := f.WriteString(strings.Join(str,"")); err != nil {
fmt.Println(err)
}
}

for _, ip := range ips {
fmt.Println(host, " - ",ip.String())
}
elapsed := time.Since(start)
fmt.Println("Hostname resolved in ", elapsed)
return true
}

func main() {
arg_len:= len(os.Args[1:])
if arg_len == 0 {
fmt.Println("Golang DNS Tester v1.0")
fmt.Println("Usage: dnstest <delay> <host1> <host2> <host3> ...\n")
} else {
timeDelay, _ := strconv.Atoi(os.Args[1])
fmt.Println("Testing for ", arg_len - 1, "hostnames\n")
for {
globalStart := time.Now()
for _, r := range os.Args[2:] {
lookUp(r)
}
globalElapsed := time.Since(globalStart)
fmt.Println("Global Elapsed Time", globalElapsed, "\n")
time.Sleep(time.Duration(timeDelay) * time.Second)
}
}
}

GitHub Repository: https://github.com/OvidiuBorlean/go-lookup

--

--