วันจันทร์ที่ 3 ธันวาคม พ.ศ. 2561

Dynu Script mikrotik

# get the current IP address from the internet
/tool fetch mode=http address="checkip.dynu.com" src-path="/" 
dst-path="/dynu.checkip.html"
:local result [/file get dynu.checkip.html contents]

# parse the current IP result
:local resultLen [:len $result]
:local startLoc [:find $result ": " -1]
:set startLoc ($startLoc + 2)
:local currentIP [:pick $result $startLoc $resultLen]
:global ddnsuser your_Dynu_username
:global ddnspass "your_Dynu_password"
:global ddnshost "your_Dynu_hostname"
:global ipddns [:resolve $ddnshost];

#:global ipddns

:if ($ipddns != $currentIP) do={
:log info ("DynuDDNS: IP-Dynu = $ipddns")
:log info ("DynuDDNS: IP-Fresh = $currentIP")
:log info "DynuDDNS: Update IP needed, Sending UPDATE...!"
:global str "/nic/update?hostname=$ddnshost&myip=$currentIP"
:log info "currentIP is $currentIP"
/tool fetch address=api.dynu.com src-path=$str mode=http user=$ddnsuser password=$ddnspass 
dst-path=("/Dynu.".$ddnshost)
:delay 1
:global str [/file find name="Dynu.$ddnshost"];
/file remove $str
:global ipddns $currentIP
:log info "DynuDDNS: IP updated to $currentIP!"
} else={
:log info "DynuDDNS: No change needed";
}


/system script
add name=Dynu
policy=read,write,test
source=":global ddnsuser your_Dynu_username
:global ddnspass "your_Dynu_password"
:global theinterface "WAN_Interface_Name"
:global ddnshost "your_Dynu_hostname"
:global ipddns [:resolve $ddnshost];
:global ipfresh [ /ip address get [/ip address find interface=$theinterface ] address ]
:if ([ :typeof $ipfresh ] = nil ) do={
:log info ("DynuDDNS: No IP address on $theinterface .")
} else={
:for i from=( [:len $ipfresh] - 1) to=0 do={
:if ( [:pick $ipfresh $i] = "/") do={
:set ipfresh [:pick $ipfresh 0 $i];
}
}
:if ($ipddns != $ipfresh) do={
:log info ("DynuDDNS: IP-Dynu = $ipddns")
:log info ("DynuDDNS: IP-Fresh = $ipfresh")
:log info "DynuDDNS: Update IP needed, Sending UPDATE...!"
:global str "/nic/update?hostname=$ddnshost&myip=$ipfresh"
/tool fetch address=api.dynu.com src-path=$str mode=http user=$ddnsuser password=$ddnspass dst-path=("/Dynu.".$ddnshost)
:delay 1
:global str [/file find name="Dynu.$ddnshost"];
/file remove $str
:global ipddns $ipfresh
:log info "DynuDDNS: IP updated to $ipfresh!"
} else={
:log info "DynuDDNS: dont need changes";
} }"


/system scheduler add comment="Update Dynu DDNS" interval=5m name=ddns_sheduller \
on-event="/system script run Dynu\r\n" policy=read,write,test start-time=startup




///////////////work


# get the current IP address from the internet
/tool fetch mode=http address="checkip.dynu.com" src-path="/" dst-path="/dynu.checkip.html"
:local result [/file get dynu.checkip.html contents]

# parse the current IP result
:local resultLen [:len $result]
:local startLoc [:find $result ": " -1]
:set startLoc ($startLoc + 2)
:local currentIP [:pick $result $startLoc $resultLen]
:global ddnsuser "user"
:global ddnspass "pass"
:global ddnshost "host"
:global ipddns [:resolve $ddnshost];

#:global ipddns

:if ($ipddns != $currentIP) do={
:log info ("DynuDDNS: IP-Dynu = $ipddns")
:log info ("DynuDDNS: IP-Fresh = $currentIP")
:log info "DynuDDNS: Update IP needed, Sending UPDATE...!"
:global str "/nic/update?hostname=$ddnshost&myip=$currentIP"
:log info "currentIP is $currentIP"
/tool fetch address=api.dynu.com src-path=$str mode=http user=$ddnsuser password=$ddnspass dst-path=("/Dynu.".$ddnshost)
:delay 1
:global str [/file find name="Dynu.$ddnshost"];
/file remove $str
:global ipddns $currentIP
:log info "DynuDDNS: IP updated to $currentIP!"
} else={
:log info "DynuDDNS: No change needed";
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น