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是否有变化,哈哈!你们自个儿玩去吧