[golang]Go net.lookup 패키지

5712 단어
DNS(Domain Name System)의 역할은 도메인 이름에 따라 IP 주소를 찾는 것이 매우 간단합니다.도메인네임시스템(일반적으로'DNS'라고 부른다)은 네트워크 시스템으로 인류에 대한 우호적인 이름을 유일한 주소로 해석할 수 있다.인터넷상의 모든 컴퓨터는 스마트폰이나 노트북에서 대량의 소매 사이트 콘텐츠를 제공할 수 있는 서버까지 번호를 사용하여 다른 쪽을 찾아 서로 통신한다.이러한 번호를 IP 주소라고 합니다.웹 브라우저를 열고 웹 사이트로 이동할 때 긴 번호를 기억하고 입력할 필요가 없습니다.대신 도메인 이름(example.com)을 입력하고 정확한 IP 주소에서 데이터를 가져옵니다.너는 그것을 거대한 전화번호부로 상상할 수 있다.다음 그림은 DNS의 작업 원리를 보여 줍니다. 

Go 언어 중

package main

import (
    "fmt"
    "net"
)

func main() {
    // DNS A 
    iprecords, _:=net.LookupIP("landv.cn")
    for _,ip:=range iprecords{
        fmt.Println(ip)
    }
    // DNS CNAME 
    canme, _:=net.LookupCNAME("www.baidu.com")
    fmt.Println(canme)
    // DNS PTR 
    ptr,e :=net.LookupAddr("8.8.8.8")
    if e != nil {
        fmt.Println(e)
    }
    for _, ptrval:=range ptr{
        fmt.Println(ptrval)
    }
    // DNS NS 
    nameserver,_:=net.LookupNS("baidu.com")
    for _,ns :=range nameserver{
        fmt.Println("ns ",ns)
    }
    // DNS MX 
    mxrecods,_ :=net.LookupMX("google.com")
    for _,mx :=range mxrecods{
        fmt.Println("mx:",mx)
    }
    // DNS TXT 
    txtrecords, _ := net.LookupTXT("baidu.com")

    for _, txt := range txtrecords {
        fmt.Println("txt:",txt)
    }
}

실행 결과
185.199.111.153
185.199.108.153
185.199.109.153
185.199.110.153
www.a.shifen.com.
dns.google.
ns  &{dns.baidu.com.}
ns  &{ns7.baidu.com.}
ns  &{ns2.baidu.com.}
ns  &{ns3.baidu.com.}
ns  &{ns4.baidu.com.}
mx: &{aspmx.l.google.com. 10}
mx: &{alt1.aspmx.l.google.com. 20}
mx: &{alt2.aspmx.l.google.com. 30}
mx: &{alt3.aspmx.l.google.com. 40}
mx: &{alt4.aspmx.l.google.com. 50}
txt: v=spf1 include:spf1.baidu.com include:spf2.baidu.com include:spf3.baidu.com a mx ptr -all
txt: google-site-verification=GHb98-6msqyx_qqjGl5eRatD3QTHyVB6-xQ3gJB5UwM

좋은 웹페이지 즐겨찾기