noip.cn是青岛每步提供的一个DDNS域名,更新方便,使用简单。刚好我手头有Mikrotik RB2011, 所有就琢磨了一下自动更新DDNS。
实现功能:
自动每隔一段时间(计划任务设定)从公网web 服务器获取一次本机wan ip, 然后和DDNS当前解析比较,如果相同,提示DDNS no changed,如果不同,则进行更新,根据返回结果输出提示。
获取公网ip:
http://ipv4.icanhazip.com/
直接返回当前公网ip地址
自动更新DDNS:
http://ipv4.meibu.com/ipv4.asp?name=your-domain.noip.cn&pwd=yourpassword
返回结果:
chenggong >> DDNS更新成功
err1 >> DDNS密码错误
err2 >> DDNS域名不存在
guoqi >> DDNS域名已过期
chongfu >> DDNS已经是最新,请勿重复更新
以下是干货,上脚本:
:local dnsIP [:resolve "your-domain.noip.cn"];
:local currenIP [:pick [/tool fetch url="http://ipv4.icanhazip.com/" as-value output=user] 0];
:local trueIP;
:local updateResult;:for i from=0 to=([:len $currenIP] - 1) do={
:local char [:pick $currenIP $i];
:if ($char = "\n") do={
:set $char "";
}
:set trueIP ($trueIP . $char);
}:if ([:toip $trueIP;] != $dnsIP) do={
"]+8) to=([:len $response]-1) do={
:local response [:pick [/tool fetch url="http://ipv4.meibu.com/ipv4.asp\?name=your-domain.noip.cn&pwd=your-password" as-value output=user] 0];
:for i from=([:find $response "
:local char [:pick $response $i];
:set updateResult ($updateResult . $char);
}
:if ($updateResult = "chongfu") do={ :log warning "DDNS update result: \"Abuse update! The domain was update to date!\"";}
:if ($updateResult = "err1") do={ :log error "DDNS update result: \"Error, the password is incorrect!\"";}
:if ($updateResult = "err2") do={ :log error "DDNS update result: \"Error, the domain was not exist!\"";}
:if ($updateResult = "daoqi") do={ :log error "DDNS update result: \"Error, the domain was expired!\"";}
:if ($updateResult = "chenggong") do={ :log info "DDNS update result: \"Success, the domain is update to date!\"";}
} else {
:log info "DDNS no Changed";
}
脚本测试好之后,就可以在System - Scheduler中添加一个计划任务,定期执行就可以,建议1分钟执行一次。