# This script runs when the status (searching, bound.. etc) of the DHCP client changes # This could be due to a change of the IPv6 prefix received from upstream (the ISP or an upstream router) # If the prefix has changed as well, an email will be sent to report this. # Note! This script needs to be put in IPv6>DHCP Client>(select client)>Script (not in System>Scripts as for other scripts). # Begun 20 September 2021, Denis de Castro, Biomedical Consulting, BMCP@pm.me. Please send corrections and improvements # Works on RouterOS v6.49.7 (stable) # Email should be already set up in Tools>Email # Please put the email address to send the report to here: :local addressto "yourname@domain.tld" # Report version 3, 16 November 2022 (please copy your own version number into the next line if necessary) :local reportVersion "3" # Note. If the routerBOARD has been restarted, there will be no # record of the old prefix in the global variable '$currentPrefix' # The next activation of this script will then throw an alert. # You can get around this by running a regular script 'on startup' # which populates the value of this global variable # Note however that a new prefix at startup will not be detected. :delay 10s; # delay so other script can run first after reboot # set up variables :local deviceName [/system identity get name]; :local hwModel [/system routerboard get model]; :local date [/system clock get date]; :local day [ :pick $date 4 6 ]; :local month [ :pick $date 0 3 ]; :local year [ :pick $date 7 11 ]; :local deviceTime [/system clock get time]; :local timeZone [/system clock get time-zone-name]; :local upTime ([/system resource get uptime]); # check if prefix has changed, and if so prepare and send an alert :global currentPrefix; :local newPrefix $"pd-prefix" :if ($newPrefix != $currentPrefix) do={ # compile report :local reportBody "$day $month $year $deviceTime $timeZone\n"; :set $reportBody ($reportBody . "Uptime (days:hours:minutes): $upTime\n\n") :set $reportBody ($reportBody . "Attention. The prefix delegated from upstream has changed"); :set $reportBody ($reportBody . " for $deviceName, a Mikrotik $hwModel\n"); :set $reportBody ($reportBody . "The prefix supplied was previously ". $currentPrefix . "\n"); :set $reportBody ($reportBody . "It is now " . $newPrefix . "\n"); :set $reportBody ($reportBody . "Fixed routing addresses may need to be changed.\n\n\n"); :set $reportBody ($reportBody . "This notification comes from a script (v. $reportVersion) within the IPv6 DHCP-client\n") :set $currentPrefix [$newPrefix] # send message :local emailSubj ($deviceName . ": IPv6 Prefix changed"); /tool e-mail send subject=$emailSubj to=$addressto body=$reportBody; :log info "***Alert sent: new IPv6 Prefix $newPrefix***" } else={ :log info "*DHCP client status changed but IPv6 prefix is unchanged. No alert sent*"; }