分享

golang 自更新DDNS脚本

 vov369 2017-05-18

golang 自更新DDNS脚本

一、首先你得从https://www./ 去注册一个账号,并申请一个二级域名

二、代码

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"time"
)

//My ip info
type IpInfo struct {
	IpAdd    string
	IsUpdate bool
	GetTime  time.Time
}

var MYIP [1]*IpInfo

func 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 = newIP
		MYIP[0].IsUpdate = false
		MYIP[0].GetTime = time.Now()
	}

	if !MYIP[0].IsUpdate {
		updateIP()
	}
}

func updateIP() {
	url := fmt.Sprintf("%s%s", "https://xxxx@xxxx.com:xxxx@www./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() {
	//Initialization
	MYIP[0] = new(IpInfo)
	for {
		newIP := getMyIp("http://myip.")
		checkIP(newIP)
		time.Sleep(300 * time.Second)
	}

}

三、说明 :将代码中的xxxx更换为你在DNSdynamic网站上的信息,即可

四、运行,每五分钟检查一下IP是否有变化,哈哈!你们自个儿玩去吧

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多