加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang 自更新DDNS脚本

发布时间:2020-12-16 18:53:51 所属栏目:大数据 来源:网络整理
导读:golang 自更新DDNS脚本 一、首先你得从 https://www.dnsdynamic.org/ 去注册一个帐号,并申请一个二级域名 二、代码 packagemainimport("fmt""io/ioutil""net/http""strings""time")//MyipinfotypeIpInfostruct{IpAddstringIsUpdateboolGetTimetime.Time}var

golang 自更新DDNS脚本

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

二、代码

packagemain

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

//Myipinfo
typeIpInfostruct{
	IpAddstring
	IsUpdatebool
	GetTimetime.Time
}

varMYIP[1]*IpInfo

funcgetMyIp(urlsstring)string{
	myip:=""
	reqest,err:=http.Get(urls)
	iferr==nil{
		deferreqest.Body.Close()
		b,_:=ioutil.ReadAll(reqest.Body)
		returnstring(b)
	}
	returnmyip
}

funccheckIP(newIPstring){
	ifnewIP!=MYIP[0].IpAdd{
		MYIP[0].IpAdd=newIP
		MYIP[0].IsUpdate=false
		MYIP[0].GetTime=time.Now()
	}

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

funcupdateIP(){
	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)
	iferr!=nil{
		return
	}
	deferreqest.Body.Close()
	b,_:=ioutil.ReadAll(reqest.Body)
	spiltB:=strings.Split(string(b),"")
	ifspiltB[0]=="good"{
		MYIP[0].IsUpdate=true
	}
}

funcmain(){
	//Initialization
	MYIP[0]=new(IpInfo)
	for{
		newIP:=getMyIp("http://myip.dnsdynamic.org")
		checkIP(newIP)
		time.Sleep(300*time.Second)
	}

}

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读