对于nslookup命令,它有nslookup somewhere.com some.dns.server.
但是,似乎golang dnsclient只从/etc/resolv.conf加载配置
代码在这里:https://golang.org/src/net/dnsclient_unix.go#L225
golang标准库是否提供类似的功能
func LookupTXT(name string,dnsServer string)(txt [] string,err error)?
需求:
1.不要更改默认的/etc/resolv.conf.
@holys
“github.com/miekg/dns is too heavy for me”
它并不那么沉重:
package main import ( "log" "github.com/miekg/dns" ) func main() { target := "microsoft.com" server := "8.8.8.8" c := dns.Client{} m := dns.Msg{} m.SetQuestion(target+".",dns.TypeA) r,t,err := c.Exchange(&m,server+":53") if err != nil { log.Fatal(err) } log.Printf("Took %v",t) if len(r.Answer) == 0 { log.Fatal("No results") } for _,ans := range r.Answer { Arecord := ans.(*dns.A) log.Printf("%s",Arecord.A) } }
运行时,您应该看到:
$go run dns.go 2015/07/26 00:24:46 Took 16.138928ms 2015/07/26 00:24:46 134.170.188.221 2015/07/26 00:24:46 134.170.185.46