Community discussions

MikroTik App
 
ezhangiso
newbie
Topic Author
Posts: 28
Joined: Tue Feb 23, 2021 9:07 am

The condition is not met, why does the script keep executing?

Wed Mar 16, 2022 3:34 pm

Hello,

The basic routing settings are shown in the attached picture. I edited a script to modify the IP Route when the gateway IP obtained by the DHCP-Client changes. In actual use, it is found that even if the conditions are not met, the gateway of the IP Route will still be modified repeatedly. Why? ?
script:”Update gateway”

{
:local newip
:local oldip
:local status
:local x "2"
:for i from=1 to=$x do={
:set status [/ip dhcp-client get [/ip dhcp-client find where interface=("wlan".$i)] status]
:if ($status="bound") do={
:set newip [/ip dhcp-client get [/ip dhcp-client find where interface=("wlan".$i)] gateway]
:set oldip [/ip route get [/ip route find dst-address=0.0.0.0/0 static=yes active=yes distance=1 comment=("tel-".$i)] gateway]
:set oldip [pick $oldip 0 12]}
:if ($newip != $oldip) do={
/ip route set [/ip route find comment=("tel-".$i)] gateway=($newip."%".("wlan".$i))
/ip route set [/ip route find comment=("tel-"."$i$i$i")] gateway=($newip."%".("wlan".$i))}}}
}
You do not have the required permissions to view the files attached to this post.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: The condition is not met, why does the script keep executing?

Wed Mar 16, 2022 7:18 pm

It seem that your picture were added as thumbnail. Not able to see details.
 
ezhangiso
newbie
Topic Author
Posts: 28
Joined: Tue Feb 23, 2021 9:07 am

Re: The condition is not met, why does the script keep executing?

Thu Mar 17, 2022 5:09 am

Hello,

Thanks for the reply, I will upload the screenshot again now.
You do not have the required permissions to view the files attached to this post.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: The condition is not met, why does the script keep executing?

Sat Mar 19, 2022 2:02 pm

Please consider to correctly indent/format the script or is unreadable

Picking with fixed size, for example,
on tel-1 give 192.168.43.1 and on tel-2 give 192.168.43.82 with missing "2" and "192.168.43.82" is different to "192.168.43.8"
or
on tel-2 give 192.168.43.82 and on tel-1 give 192.168.43.1% with added "%" and "192.168.43.1" is different to "192.168.43.1%"

You must split at %, not a generic position
# simply replace
:set oldip [pick $oldip 0 12]}

# with
:set oldip [:pick $oldip 0 [:find $oldip "%" -1]]}
Last edited by rextended on Sat Mar 19, 2022 2:16 pm, edited 2 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: The condition is not met, why does the script keep executing?

Sat Mar 19, 2022 2:13 pm

Fixed version
:local newip
:local oldip
:local status
:local x 2
:for i from=1 to=$x do={
    /ip dhcp-client
    :set status [get [find where interface="wlan$i"] status]
    :if ($status = "bound") do={
        /ip dhcp-client
        :set newip [get [find where interface="wlan$i"] gateway]
        /ip route
        :set oldip [:tostr [get [find where dst-address=0.0.0.0/0 static=yes active=yes distance=1 comment="tel-$i"] gateway] ]
        :set oldip [:pick $oldip 0 [:find $oldip "%" -1]]
    }
    :if ($newip != $oldip) do={
        /ip route
        set [find where comment="tel-$i"] gateway="$newip%wlan$i"
        set [find where comment="tel-$i$i$i"] gateway="$newip%wlan$i"
    }
}
Last edited by rextended on Sat Mar 19, 2022 2:38 pm, edited 4 times in total.
 
ezhangiso
newbie
Topic Author
Posts: 28
Joined: Tue Feb 23, 2021 9:07 am

Re: The condition is not met, why does the script keep executing?

Sat Mar 19, 2022 2:25 pm

Hello,

Thanks for the guidance, but the script keeps getting executed.
You do not have the required permissions to view the files attached to this post.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: The condition is not met, why does the script keep executing?

Sat Mar 19, 2022 2:31 pm

I check now, probably is data conversion...

"find" everytime return an array (from 0 to x values) ... I fix previous post adding [:tostr ... ]
 
ezhangiso
newbie
Topic Author
Posts: 28
Joined: Tue Feb 23, 2021 9:07 am

Re: The condition is not met, why does the script keep executing?

Sat Mar 19, 2022 3:20 pm

Hello,

Thank you very much, the test passed.

Who is online

Users browsing this forum: alexantao and 19 guests