为什么golang Lookup*** 函数不能提供服务器参数? [英] Why golang Lookup*** function can't provide a server parameter?

查看:25
本文介绍了为什么golang Lookup*** 函数不能提供服务器参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于nslookup 命令,它有nslookup where.com some.dns.server.

然而,似乎golang dnsclient 只从/etc/resolv.conf

However, it seems that golang dnsclient only load config from /etc/resolv.conf

代码在这里:https://golang.org/src/net/dnsclient_unix.go#L225

golang 标准库是否提供类似的东西func LookupTXT(name string, dnsServer string) (txt []string, err error) ?

Does the golang standard library provide something like func LookupTXT(name string, dnsServer string) (txt []string, err error) ?

要求:1.不要改变默认的/etc/resolv.conf.

requirement: 1. Don't change the default /etc/resolv.conf.

推荐答案

@holys

github.com/miekg/dns 对我来说太重了"

"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

这篇关于为什么golang Lookup*** 函数不能提供服务器参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆