国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

所在位置: 首頁(yè) > 營(yíng)銷(xiāo)資訊 > 建站知識(shí) > 檢測(cè)外網(wǎng)IP變化,動(dòng)態(tài)更新阿里云域名解析

檢測(cè)外網(wǎng)IP變化,動(dòng)態(tài)更新阿里云域名解析

時(shí)間:2023-02-19 23:44:01 | 來(lái)源:建站知識(shí)

時(shí)間:2023-02-19 23:44:01 來(lái)源:建站知識(shí)

檢測(cè)外網(wǎng)IP變化,動(dòng)態(tài)更新阿里云域名解析:置辦寬帶后,運(yùn)營(yíng)商一般會(huì)給貓(路由器)的WAN網(wǎng)口分配一個(gè)外網(wǎng)IP。再通過(guò)端口映射(port forwarding)功能,就能遠(yuǎn)程ssh訪(fǎng)問(wèn)家里的設(shè)備(PC/樹(shù)莓派)或者將其作為服務(wù)器使用。為了方便,我將我的域名@.aaron-xin.tech解析到了該外網(wǎng)IP。使用一段時(shí)間后,突然有一天,突然所有服務(wù)都無(wú)法訪(fǎng)問(wèn)了,但是IP還是能夠ping通。排查后發(fā)現(xiàn),外網(wǎng)IP也是通過(guò)DHCP的方式獲取的,意味著每次lease更新的時(shí)候,有比較大的概率IP會(huì)發(fā)生變化。所以,我通過(guò)家中的樹(shù)莓派實(shí)時(shí)(每小時(shí))監(jiān)控外網(wǎng)IP的變化,并根據(jù)新的外網(wǎng)IP調(diào)用阿里云API更新域名解析。

目錄

  1. 獲取域名解析IP
  2. 獲取實(shí)時(shí)外網(wǎng)IP
  3. 更新阿里云域名解析
  4. 開(kāi)機(jī)自動(dòng)運(yùn)行腳本
可以看出,WAN口IP地址需要通過(guò)DHCP獲取,并且lease time為一天

獲取域名解析地址

第一步,獲取域名解析到的IP地址(舊地址)。首先想到用最常見(jiàn)的ping命令,再?gòu)?code>ping的輸出中解析出想要的IP地址。

但是這種辦法有一點(diǎn)風(fēng)險(xiǎn),如果舊的IP地址沒(méi)有被DHCP分配給別人,就會(huì)出現(xiàn)ping不通的情況。所以最自然最保險(xiǎn)的辦法應(yīng)該是去做DNS Lookup,對(duì)應(yīng)的Linux Command就是dig <domain name>:

可以通過(guò)左邊的方式,直接從最近的DNS server,也就是192.168.0.1#53查詢(xún)。也可以指定8.8.8.8#53(Google的DNS server)。

對(duì)應(yīng)的Golang代碼為:

func getResolver() *net.Resolver { return &net.Resolver{ PreferGo: true, Dial: func(ctx context.Context, network, address string) (net.Conn, error) { d := net.Dialer{ Timeout: time.Millisecond * time.Duration(10000), } return d.DialContext(ctx, "udp", "8.8.8.8:53") }, }}// DNS lookupips, _ := resolver.LookupHost(context.Background(), "aaron-xin.tech")dnsIP = ips[0]

獲取實(shí)時(shí)外網(wǎng)地址

第二步,實(shí)時(shí)獲取外網(wǎng)IP地址。子網(wǎng)中每一個(gè)設(shè)備都通過(guò)同一外網(wǎng)IP與外網(wǎng)鏈接。因?yàn)槊看蜨TTP/HTTPs的GET都會(huì)包含該請(qǐng)求的外網(wǎng)IP地址,網(wǎng)上有很多網(wǎng)站利用此功能提供”IP Lookup“的功能。如我使用的ifconfig.me:

對(duì)應(yīng)的Golang代碼為:

var realIP string// Get real IPresp, err := http.Get("http://ifconfig.me") // the ip discover service, choose a nearby oneif err == nil { defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) realIP = string(body)} else { log.Println("Fail to get ip from ifconfig.me")}

更新阿里云域名解析

第三步,更新阿里云域名解析。這個(gè)需要通過(guò)調(diào)用阿里云的域名解析API完成。

阿里云云解析DNS調(diào)用TOP5的接口:

  1. 通過(guò) AddDomainRecord 根據(jù)傳入?yún)?shù)添加解析記錄。
  2. 通過(guò) UpdateDomainRecord 根據(jù)傳入?yún)?shù)修改解析記錄。
  3. 通過(guò) DescribeDomainRecords 根據(jù)傳入?yún)?shù)獲取指定主域名的所有解析記錄列表。
  4. 通過(guò) DescribeSubDomainRecords 根據(jù)傳入?yún)?shù)獲取某個(gè)固定子域名的所有解析記錄列表。
  5. 通過(guò) DescribeDomainRecordInfo 根據(jù)傳入?yún)?shù)獲取某個(gè)固定子域名下的解析記錄信息。
這里我們需要用到的是UpdateDomainRecordDescribeDomainRecords。通過(guò)DescribeDomainRecord獲取對(duì)應(yīng)域名解析記錄的Record ID,使用Record ID作為參數(shù)調(diào)用UpdateDomainRecord修改對(duì)應(yīng)解析記錄。

func descRecords(domain string) (string, error)func updateDomainRecord(ipAddr string, recordID string) errorif dnsIP != realIP { log.Println("IPs do not match, requesting DNS change...") recordID, err := descRecords(domain) if err != nil { log.Fatal(err) } err = updateDomainRecord(realIP, recordID) if err != nil { log.Fatal(err) }}完整代碼可參考 dynamicDNS.go

開(kāi)機(jī)自動(dòng)運(yùn)行腳本

最后一步,開(kāi)機(jī)自動(dòng)運(yùn)行腳本。這個(gè)腳本我跑在樹(shù)莓派上,樹(shù)莓派裝的系統(tǒng)是ubuntu@20.14 Server。因?yàn)闃?shù)莓派可能會(huì)存在突然宕機(jī)的可能,如果每次手動(dòng)運(yùn)行腳本比較麻煩。也有可能在樹(shù)莓派宕機(jī)的時(shí)候,外網(wǎng)IP剛好發(fā)生了變化。因此,需要在樹(shù)莓派每次開(kāi)機(jī)后在后臺(tái)自動(dòng)運(yùn)行檢查IP變化的腳本。開(kāi)機(jī)自動(dòng)運(yùn)行的方式有很多種,這里我選擇使用了編寫(xiě)/etc/rc.local腳本的方式。/etc/rc.local是一個(gè).sh腳本,會(huì)在系統(tǒng)啟動(dòng)后root身份執(zhí)行命令。如果沒(méi)有這個(gè)文件,可以通過(guò)sudo touch /etc/rc.local創(chuàng)建或者sudo vim /etc/rc.local創(chuàng)建并編輯。

#!/bin/sh -e## rc.local## This script is executed at the end of each multiuser runlevel.# Make sure that the script will "exit 0" on success or any other# value on error.## In order to enable or disable this script just change the execution# bits.## By default this script does nothing.# Logexec 2> /tmp/rc.local.log # send stderr from rc.local to a log fileexec 1>&2 # send stdout to the same log fileset -x # tell sh to display commands before execution# Run the dynamic dns update servicerunuser -u ubuntu -- /usr/bin/tmux new-session -s dns -d /usr/local/go/bin/go run /home/ubuntu/go/src/github.com/Airine/dynamic-dns/main.go# Endexit 0關(guān)于后臺(tái)運(yùn)行,我使用了tmux這個(gè)工具(一款優(yōu)秀的終端復(fù)用軟件)。由于/etc/rc.local會(huì)使用root身份執(zhí)行,而我的tmuxgo都屬于ubuntu。這里使用了runuser -u <username> -- <command>命令,可以以任意user身份執(zhí)行--后的命令。

推薦

  1. 原文lang dynamicDNS.go
  2. 開(kāi)機(jī)啟動(dòng)腳本 rc.local.sh
  3. tmux 使用手冊(cè)
原文:



關(guān)鍵詞:更新,動(dòng)態(tài),變化

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點(diǎn)擊下載Chrome瀏覽器
關(guān)閉