Hi again!
I have a very annoying problem. I’m trying to write a script that will send an email when my WAN IP changes. Now, there are plenty of search results for this, but the way basically seems to be to schedule a script to run every so often, which will compare the current IP of the WAN interface with one previously stored in a global variable. This seems to work reliably. BUT, I thought, why not start the script from the DHCP-client script window, when bound is detected? Then I would get instantly notified if my public IP is changed and I could just schedule the script to run say once an hour (just as a backup really). However, this doesn’t seem to work most of the time. I have included in the script (which I’ll post below) a line to send the new IP to the log and also on the last line to send a message to the log that the script has finished successfully. In the log both the new IP (which will also be “new” after a reboot, since my “wan_ip” variable isn’t yet stored) and the message that the script finished, appears. However when I log into WinBox, and check the “Environment” tab in “Script List”, the variable doesn’t exist. Only sometimes, seemingly randomly, it does! I read in some post on this forum that scripts run from DHCP-client, doesn’t have the required permissions, so according to that post (which I can’t find now), I checked the “Don’t Require Permissions” checkbox. But this didn’t make it work either. If I put a delay before calling the script, so I have the time to log in before it’s run, it seems to work, but I’m not sure about this. On the other hand, it seems it’s SOMETIMES working if it’s run before I’m logged in. But most of the time it doesn’t. Oh, and if I run the script manually, by “/system/script run check_wan_ip”, it always works. Any suggestions? Because it would be nice if it could RELIABLY be triggered by the DHCP-client itself. Then I could possibly even do ":global “wan_ip” $“lease-address” to get the WAN IP, before calling my script, thus enabling a much simplified script.
My main script:
# Set WAN interface name.
:local "wan_interface" "WAN-bridge"
# Define "wan_ip" variable, in which the current IP will be stored.
:global "wan_ip"
# Get the current IP on the interface.
:local "current_wan_ip_full" [/ip/address get [find interface=$"wan_interface"] address]
# Strip netmask.
:local "current_wan_ip" [:pick $"current_wan_ip_full" 0 [:find $"current_wan_ip_full" "/"]]
# Check if IP has changed.
:if ($"current_wan_ip" != $"wan_ip") do={
# Write info to log.
/log info "WAN IP changed from $"wan_ip" to $"current_wan_ip""
# Update the "wan_ip" variable to the new IP.
:set "wan_ip" $"current_wan_ip"
# /system script run email_ip
/log info "wan_ip script finished"
}
The code in DHCP-client:
:if ($bound=1) do={
# :delay 20
/system/script/ run check_wan_ip
}
All the best!