Skip to content

Instantly share code, notes, and snippets.

@shicky
Created August 26, 2016 04:24
Show Gist options
  • Save shicky/0ac40e07a93ff813c9b04b850c717091 to your computer and use it in GitHub Desktop.
Save shicky/0ac40e07a93ff813c9b04b850c717091 to your computer and use it in GitHub Desktop.
package main
import "github.com/miekg/dns"
// ResolveIP resolves IP address using provided DNS server
func ResolveIP(url, server string) (string, error) {
msg := new(dns.Msg)
msg.Id = dns.Id()
msg.RecursionDesired = true
msg.Question = make([]dns.Question, 1)
msg.Question[0] = dns.Question{url + ".", dns.TypeA, dns.ClassINET}
in, err := dns.Exchange(msg, fmt.Sprintf("%s:53", server))
if err != nil {
return "", err
}
answers := strings.Split(in.Answer[0].String(), "\t")
return answers[len(answers)-1], nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment