-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnstestv2.go
67 lines (62 loc) · 1.77 KB
/
dnstestv2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"net"
"fmt"
"os"
"time"
"strings"
_"log"
"strconv"
"context"
)
var host string = ""
func lookUp(host string) bool {
start := time.Now()
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Millisecond)
//if cancel != nil {
// fmt.Println("cancel")
// }
//ips, err := net.LookupIP(host)
defer cancel() // important to avoid a resource leak
var r net.Resolver
ips, err := r.LookupIP(ctx, "ip4",host)
if err != nil {
fmt.Fprintf(os.Stderr, "->Could not get IPs: %v\n", err)
//os.Exit(1)
f, ferr := os.OpenFile("dnsreq.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if ferr != nil {
fmt.Println(err)
}
defer f.Close()
timeString := start.Format("2006-01-02 15:04:05")
str := []string{timeString, " ", host, " ", err.Error(), "\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)
}
}
}