Nslookup.go 631 B

123456789101112131415161718192021222324252627282930313233
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/miekg/dns"
  5. )
  6. func nslookup(targetAddress, server string) (res []string) {
  7. if server == "" {
  8. server = "8.8.8.8"
  9. }
  10. c := dns.Client{}
  11. m := dns.Msg{}
  12. m.SetQuestion(targetAddress+".", dns.TypeA)
  13. ns := server + ":53"
  14. r, t, err := c.Exchange(&m, ns)
  15. if err != nil {
  16. fmt.Printf("nameserver %s error: %v\n", ns, err)
  17. return res
  18. }
  19. fmt.Printf("nameserver %s took %v", ns, t)
  20. if len(r.Answer) == 0 {
  21. return res
  22. }
  23. for _, ans := range r.Answer {
  24. if ans.Header().Rrtype == dns.TypeA {
  25. Arecord := ans.(*dns.A)
  26. res = append(res, fmt.Sprintf("%s", Arecord))
  27. }
  28. }
  29. return
  30. }