Hi,
I’m relatively new to MT scripting. I got from this forum a dynDNS script that I adapted to be more flash compliant, but I still see it sometimes going “into a loop”, I mean it doesn’t end. Too bad when it happens because I then lost my MT device on the interweb
(450G v4.17)
Then I came to think about what are good practices when it comes to deal with MT scripts. I mean, like:
- be sure a variable is set before testing its value
- use a global variable instead of a file on flash
- set a lock in a global variable if you don’t know your script can have 2 instances at the same time
- etc
But still, I see one of my both script (one for dynDNS, one to mail me the new IP address to a mailbox) never ending from times to times, without being able to say if it’s my scripts fault or not. For example, the second script (mailing IP addr) lock variable was set and there was a job running but no mail where sent. I manually reset the lock variable, the script was ran again by scheduler and mail was correctly sent this time.
So please feel free to comment further a list of good practices, and if you have an idea of what may be wrong, I write both scripts bellow.
Thanks for your advice ![]()
0 name="script-DynDNS" owner="elgo" policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive last-started=may/25/2011 13:54:36 run-count=12774
source=
# Set needed variables
:local username "fezrzerez"
:local password "rezrezr"
:local hostname "ileerezrzes.org"
:local waninterface "pppoe-fibre"
:global dyndnsForce
:global previousIP
:global dyndnsLock
# print some debug info
#:log info ("UpdateDynDNS: username = $username")
#:log info ("UpdateDynDNS: password = $password")
#:log info ("UpdateDynDNS: hostname = $hostname")
# Lock pour eviter une double execution
:if ([ :typeof $dyndnsLock ] = nil ) do={
:set dyndnsLock false
}
:if ($dyndnsLock = true) do={
:error "script-dynDNS already running"
}
# set the lock
:set dyndnsLock true
# get the current IP address from
:local currentNET [/ip address get [/ip address find interface=$waninterface] address]
:local currentIP [ :pick $currentNET 0 [ :find $currentNET "/" ] ]
# Remove the # on next line to force an update every single time - useful for debugging, but you could end up getting blacklisted by DynDNS!
#:set dyndnsForce true
# Determine if dyndns update is needed
# more dyndns updater request details available at http://www.dyndns.com/developers/specs/syntax.html
# Did we get an IP address to compare?
:if ([ :typeof $currentIP ] = nil ) do={
:log info ("UpdateDynDNS: No ip address present on " . $waninterface . ", please check.")
} else={
:if (($dyndnsForce = true) || ([ :typeof $previousIP ] = nil) || ($currentIP != $previousIP)) do={
:set dyndnsForce false
:log info ("UpdateDynDNS: Dyndns update needed")
:log info ("UpdateDynDNS: previousIP = $previousIP --> currentIP = $currentIP")
:set previousIP $currentIP
/tool fetch user=$username password=$password mode=http address="members.dyndns.org" src-path="/nic/update?hostname=$hostname&myip=$currentIP" dst-p>
:local result [/file get dyndns.txt contents]
:log info ("UpdateDynDNS: Dyndns Update Result: ".$result)
:put ("Dyndns Update Result: ".$result)
} else={
# :log info ("UpdateDynDNS: No dyndns update needed")
}
}
# free the lock
:set dyndnsLock false
1 name="script-IPtoGMAIL" owner="elgo" policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive last-started=may/25/2011 13:55:28 run-count=6999
source=
# Set needed variables
:local waninterface "pppoe-fibre"
:global lastIPsent
:global IPtoGMAILLock
# Lock pour eviter une double execution
:if ([ :typeof $IPtoGMAILLock ] = nil ) do={
:set IPtoGMAILLock false
}
:if ($IPtoGMAILLock = true) do={
:error "script-IPtoGMAIL already running"
}
# set the lock
:set IPtoGMAILLock true
# get the current IP address from
:local currentNET [/ip address get [/ip address find interface=$waninterface] address]
:local currentIP [ :pick $currentNET 0 [ :find $currentNET "/" ] ]
# Determine if dyndns update is needed
# more dyndns updater request details available at http://www.dyndns.com/developers/specs/syntax.html
# Did we get an IP address to compare?
:if ([ :typeof $currentIP ] = nil ) do={
:log info ("IPtoGMAIL: No ip address present on " . $waninterface . ", please check.")
} else={
:if (([ :typeof $lastIPsent ] = nil) || ($currentIP != $lastIPsent)) do={
:log info ("IPtoGMAIL: IP change notification to be sent")
:log info ("IPtoGMAIL: lastIPsent = $lastIPsent --> currentIP = $currentIP")
/tool e-mail send to="fdsfdsfs@gmail.com" subject=([/system identity get name] . ": Nouvelle adresse = $currentIP") from="rzerezrze@gmail.com">
:set lastIPsent $currentIP
} else={
# :log info ("IPtoGMAIL: no need to sent IP change notification")
}
}
# free the lock
:set IPtoGMAILLock false