Script - Netwatch

Trying to figure out what I’m doing wrong in my script here.

I’m using 2 global variable on 2 netwatch-ed IP’s. One is an Internal IP of the ADSL modem the second is the External IP of the ADSL modem.
The scripting is supposed to automatically disable some mangle rules if the ADSL modem is unreachable on either IP address (if the ADSL modem has no power or there is no DSL sync/connection)

I’m trying to use the global variable to confirm that both IP addresses respond before the mangle rules are turned back on again but I seem to have a problem with the global variable for one not recognised on the other.

Below are the scripts I have on both netwatch entries.

On local IP Up

:global elo "1"
:if ([$elo + $dsl="2"]) do={
  :foreach i in [/ip firewall mangle find] do={
    :if ([:find [/ip firewall mangle get $i comment] "Netwatch_Toggle"]=0) do={
      /ip firewall mangle set $i disabled=no
    }
  }
}

On local IP Down

:global elo "0"
:foreach i in [/ip firewall mangle find] do={
  :if ([:find [/ip firewall mangle get $i comment] "Netwatch_Toggle"]=0) do={
    /ip firewall mangle set $i disabled=yes
  } 
}

On ADSL IP Up

:global dsl "1"
:if ([$elo + $dsl="2"]) do={
  :foreach i in [/ip firewall mangle find] do={
    :if ([:find [/ip firewall mangle get $i comment] "Netwatch_Toggle"]=0) do={
      /ip firewall mangle set $i disabled=no
    }
  }
}

On ADSL IP Down

:global dsl "0"
:foreach i in [/ip firewall mangle find] do={
  :if ([:find [/ip firewall mangle get $i comment] "Netwatch_Toggle"]=0) do={
    /ip firewall mangle set $i disabled=yes
  } 
}

On ADSL IP Up

...
:if (($elo + $dsl) = "2") do={
...
}

Thanks, final up script ended up like this:

:global elo "1"
:global dsl
:if (($elo + $dsl)="2") do={
  :foreach i in [/ip firewall mangle find] do={
    :if ([:find [/ip firewall mangle get $i comment] "Netwatch_Toggle"]=0) do={
      /ip firewall mangle set $i disabled=no
    }
  }
}
:global check ($eloa + $dsl)
:log info "Netwatch: $check"

The log entry is just so I can see whats been happening.

Hmm, script is in place but doesn’t add up correctly when an interface goes up or down.
Should I have to define the $dsl variable in each script? (Its a global variable)

Because at the moment if I don’t the script errors later when I call it.
But it would seem that defining it in the same script resets its value to blank.

Yes, if you have to define global variable in every script you want to use it.
If variable exists then ‘:global dsl’ will not reset its value.