這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
golang 自更新DDNS指令碼
一、首先你得從https://www.dnsdynamic.org/ 去註冊一個帳號,並申請一個次層網域
二、代碼
package mainimport ("fmt""io/ioutil""net/http""strings""time")//My ip infotype IpInfo struct {IpAdd stringIsUpdate boolGetTime time.Time}var MYIP [1]*IpInfofunc getMyIp(urls string) string {myip := ""reqest, err := http.Get(urls)if err == nil {defer reqest.Body.Close()b, _ := ioutil.ReadAll(reqest.Body)return string(b)}return myip}func checkIP(newIP string) {if newIP != MYIP[0].IpAdd {MYIP[0].IpAdd = newIPMYIP[0].IsUpdate = falseMYIP[0].GetTime = time.Now()}if !MYIP[0].IsUpdate {updateIP()}}func updateIP() {url := fmt.Sprintf("%s%s", "https://xxxx@xxxx.com:xxxx@www.dnsdynamic.org/api/?hostname=xxxx.dnsget.org&myip=", MYIP[0].IpAdd)reqest, err := http.Get(url)if err != nil {return}defer reqest.Body.Close()b, _ := ioutil.ReadAll(reqest.Body)spiltB := strings.Split(string(b), " ")if spiltB[0] == "good" {MYIP[0].IsUpdate = true}}func main() {//InitializationMYIP[0] = new(IpInfo)for {newIP := getMyIp("http://myip.dnsdynamic.org")checkIP(newIP)time.Sleep(300 * time.Second)}}
三、說明 :將代碼中的xxxx更換為你在DNSdynamic網站上的資訊,即可
四、運行,每五分鐘檢查一下IP是否有變化,哈哈!你們自個兒玩去吧