時(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更新域名解析。ping
命令,再?gòu)?code>ping的輸出中解析出想要的IP地址。ping
不通的情況。所以最自然最保險(xiǎn)的辦法應(yīng)該是去做DNS Lookup,對(duì)應(yīng)的Linux Command就是dig <domain name>
:192.168.0.1#53
查詢(xún)。也可以指定8.8.8.8#53
(Google的DNS server)。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]
ifconfig.me
: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")}
UpdateDomainRecord
和DescribeDomainRecords
。通過(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/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í)行,而我的tmux
和go
都屬于ubuntu
。這里使用了runuser -u <username> -- <command>
命令,可以以任意user
身份執(zhí)行--
后的命令。關(guān)鍵詞:更新,動(dòng)態(tài),變化
客戶(hù)&案例
營(yíng)銷(xiāo)資訊
關(guān)于我們
客戶(hù)&案例
營(yíng)銷(xiāo)資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。