:local statusEdok [/ip ipsec policy get [find comment="Peer-Edok"] value-name=ph2-state]
:put $statusEdok
:delay 20s
:local Edok [/ip ipsec policy get [find comment="Peer-Edok"] value-name=ph2-state]
:put $Edok
:if ($statusEdok!=$Edok) do={
:log warning "PEER EDOK $statusEdok"
}
/system script
add comment=established dont-require-permissions=yes name=state-peer-ipsec \
owner=installer policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="#-\
---------SCRIPT WRITE STATE LOG PEER IPSEC----------\r\
\n#\r\
\n# Script: WRITE STATE LOG CONNECTION PEER IPSEC\r\
\n# Version: 1.0\r\
\n# RouterOS v7.9\r\
\n# Created: 18/05/2023\r\
\n# Updated: no updated\r\
\n# Author: Fabio Fois\r\
\n# Website: https://foisfabio.it\r\
\n# Email: consulenza@foisfabio.it\r\
\n#\r\
\n#----------NO MODIFY THIS SECTION----------\r\
\n\r\
\n{\r\
\n:local scriptName \"state-peer-ipsec\"\r\
\n:local LastStatusEdok [/system script get [find name=\"\$scriptName\"] co\
mment]\r\
\n:local CurrentStatusEdok [/ip ipsec policy get [find comment=\"Peer-Edok\
\"] value-name=ph2-state]\r\
\n:put \$LastStatusEdok\r\
\n:put \$CurrentStatusEdok\r\
\n:local keepOutput false\r\
\n:local output\r\
\n:if ([:len \$LastStatusEdok] = 0) do={\r\
\n:set keepOutput true\r\
\n}\r\
\n:if (\$CurrentStatusEdok != \$LastStatusEdok) do={\r\
\n:set keepOutput true\r\
\n:set output [/system script set [find name=\"\$scriptName\"] comment=\$C\
urrentStatusEdok\r\
\n/log warning \"STATO PEER EDOK=\$CurrentStatusEdok\"]\r\
\n:if (\$CurrentStatusEdok = \$LastStatusEdok) do={\r\
\n:set keepOutput false\r\
\n:set output \"\"\r\
\n}\r\
\n:put \$keepOutput\r\
\n:if (\$keepOutput = true) do={\r\
\n\$output\r\
\n}\r\
\n\r\
\n}\r\
\n}"
/system scheduler
add interval=20s name=schedule_state-peer-ipsec on-event=\
"/system script run state-peer-ipsec" policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
start-date=may/18/2023 start-time=19:31:44
[…]
:local output
[…]
:set output [/system script set [find name="script1"] comment=$CurrentStatusEdok
/log warning "STATO PEER EDOK=$CurrentStatusEdok"]
[…]
:if ($keepOutput = true) do={
$output
}
[…]
A function is not created that way, the :set, that way, immediately executes what is written there, and the RESULT assigns it to $output.Yes obviously, I made a mistake in writing, I got confused in translating it. I'm better at scripting than English...the script runs every 20 seconds and not every 20 minutes
Come in Italia, il certificato non serve a niente, se quello che hai imparato lo hai fatto a memoria senza sapere come applicarlo e a che serve...thanks for your patience. but isn't the global variable meant to appear in the system script environment so that it can be used by other scripts? is this the only difference from the local variable or else are there other features that I didn't get to appreciate. the fact of using the comment as a support to write the variable is not my invention. It was copied from a script posted by a certified trainer on github.
{
:global LastStatusEdok
:local CurrentStatusEdok [/ip ipsec policy get [find comment="Peer-Edok"] value-name=ph2-state]
:put $LastStatusEdok
:put $CurrentStatusEdok
:local keepOutput false
:local output
:if ([:len $LastStatusEdok] = 0) do={
:set keepOutput true
}
:if ($CurrentStatusEdok != $LastStatusEdok) do={
:set keepOutput true
:set output ($LastStatusEdok)
/log warning "STATO PEER EDOK=$CurrentStatusEdok"
}
:if ($CurrentStatusEdok = $LastStatusEdok) do={
:set keepOutput false
:set $output ""
}
:put $keepOutput
:if ($keepOutput = true) do={
$output
}
}
:global LastStatusEdok
:local CurrentStatusEdok [/ip ipsec policy get [find where comment="Peer-Edok"] value-name=ph2-state]
:if ($CurrentStatusEdok != $LastStatusEdok) do={
/log warning "Lo stato del Peer-Edok cambia da $LastStatusEdok a $CurrentStatusEdok"]
:set LastStatusEdok $CurrentStatusEdok
}