Community discussions

MikroTik App
 
chriswibbs
just joined
Topic Author
Posts: 2
Joined: Tue Jul 26, 2022 10:22 pm

Address List Not Updating From Array

Wed Jul 27, 2022 3:03 am

I've got a weird problem which I can't figure out - hoping somebody else can spot my mistake...

I have three bridges configured:
  • LAN Bridge
  • WAN Primary Bridge
  • WAN Secondary Bridge

I then have two address lists configured:
  • "LAN Address" - contains any IP addresses configured on LAN Bridge
  • "WAN Address" - contains any IP addresses configured on either WAN Primary Bridge, or WAN Secondary Bridge

Each bridge interface can have zero or more IP addresses. I have two scripts, each running every 5 minutes, which should check for any IP address changes (adds, removals or changes) and updates the address lists accordingly.


The LAN Bridge script works well - it'll detect an addition/removal, and will delete the entire address list & re-add it. If it detects a change, it'll add a new record to the address list (I haven't yet got round to updating the address list record). Below is the script:
:global LANIPs;
:local NewLANIPs;
:log info ".................................................................."

:set NewLANIPs "$[/ip address find interface="LAN Bridge"]"

:if ($LANIPs != $NewLANIPs) do={
    :log info "Not equal. Updating LANIPs";
    :set LANIPs $NewLANIPs;
    /ip firewall address-list remove [find list="LAN Address"]

        
}

:foreach i in=$NewLANIPs do={
    /ip firewall address-list add list="LAN Address" address=[/ip address get $i address] comment="Dynamic Add from Script"
}

The WAN script however, isn't behaving as I'd hoped. If I create two IP addresses, one for WAN Primary Bridge, and one for WAN Secondary Bridge, and then run the script, the address list gets populated as expected. If I modify the WAN Secondary Bridge IP address (the last IP address created), the script does add a new address list record. However, if I modify anything other than the last IP address created (on either bridge interface), the script doesn't update anything. Below is the script:
    :global WANIP;
    :local NewWANIPs;

    :set NewWANIPs "$[/ip address find interface~"^(WAN).*(Bridge)"]"

    :if ($WANIP != $NewWANIPs) do={
        :log info "Not equal. Updating WANIPs";
        :set WANIP $NewWANIPs;
        /ip firewall address-list remove [find list="WAN Address"]        
    }

    :foreach i in=$NewWANIPs do={
        /ip firewall address-list add list="WAN Address" address=[/ip address get $i address] comment="Dynamic Add from Script"
    }

I'm out of ideas, any help would be really appreciated!
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Address List Not Updating From Array

Wed Jul 27, 2022 4:41 am

revised code

    :global WANIPs
# missing the s... what containing, list of IPs or list of IDs... the name suggest IPs...

# :set & "$[ are completly useless and create only confusion

    :local  NewWANIPs [/ip address find where interface~"^(WAN).*(Bridge)"]
# now inside NewWANIPs are present the IDs of the IPs, not directly the IPs
    
    :if ($WANIPs != $NewWANIPs) do={
# ok, compare list of IDs with another list of IDs (if any)
# on first run WANIPs is empty

        :log info "Not equal. Updating WANIPs"
        :set WANIPs $NewWANIPs
# now WANIPs have the same value of NewWANIPs
        /ip firewall address-list remove [find where list="WAN Address"]
    }

# why do that outside the cycle, if are identical are useless update the list.....
# and if the address is already present, cause error and the script is stopped.
    :foreach i in=$NewWANIPs do={
# for each IDs of each IPs
        /ip firewall address-list add list="WAN Address" address=[/ip address get $i address] comment="Dynamic Add from Script"
# finally the IP is used
    }

as you can "see" you never use the IP, but only the ID, and a new IP can have the identical position of old address, and the script do not notice any difference and do not update the list...

the variable must express everitime what is contained, or cause only confusion....

But let's admit that it was a list of IPs, if the list contained 1.1.1.1 and 8.8.8.8 and the next check contained 8.8.8.8 and 1.1.1.1,
despite having the same identical IPs, it was considered different.
 
chriswibbs
just joined
Topic Author
Posts: 2
Joined: Tue Jul 26, 2022 10:22 pm

Re: Address List Not Updating From Array

Wed Jul 27, 2022 8:16 am

Thanks for the reply there - and noted about the variable names!

I've edited the script based on what you suggested, however it's still resulting in the same error - if you modify one of the IP addresses (providing it's not the most recent one added), the address lists aren't updated. The updated script is below:

:global WANIPs
:local NewWANIPs [/ip address find where interface~"^(WAN).*(Bridge)"]

:if ($WANIPs != $NewWANIPs) do={
# on first run WANIPs is empty

:log info "Not equal. Updating WANIPs"
:set WANIPs $NewWANIPs
# now WANIPs have the same value of NewWANIPs
/ip firewall address-list remove [find where list="WAN Address"]
}

:foreach i in=$NewWANIPs do={
# for each IDs of each IPs
/ip firewall address-list add list="WAN Address" address=[/ip address get $i address] comment="Dynamic Add from Script"
# finally the IP is used
}

Is there something I'm still missing here?

Who is online

Users browsing this forum: infabo, JDF and 21 guests