Community discussions

MikroTik App
 
scracha
newbie
Topic Author
Posts: 25
Joined: Fri Dec 27, 2013 3:28 am

get within a nested if

Wed Oct 06, 2021 10:03 pm

Hi folks,

I'm trying to disable/enable an IP address via a script but ignore it if the IP address is already in the correct state.

However, I'm getting an issue if I nest the If statements.
if ([/ping 172.16.1.1 interface=ether2 interval=1 count=3] = 0) do={
:if ([ /ip address get [find address="172.16.2.2/32"] disabled]) do={
/ip address enable [find address="172.16.2.2/32"]
}
} else={
:if ([/ip address get [find address="172.16.2.2/32"] enabled]) do={ 
/ip address disable [find address="172.16.2.2/32"]
}
}
The error I get is
input does not match any value of value-name
So I broke it down a little and these 2 scripts work OK.

This tests the address enable/disable
if ([/ping 172.16.1.1 interface=ether2 interval=1 count=3] = 0) do={
/ip address enable [find address="172.16.2.2/32"]
} else={ 
/ip address disable [find address="172.16.2.2/32"]
}
This tests the get statement
:if ([/ip address get [find address="172.16.2.2/32"] disabled]=true) do={
  :put "The address is disabled!";
} else={
  :put "The address is enabled!";
}



Would appreciate any pointers in the right direction.

Thanks
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12003
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: get within a nested if

Thu Oct 07, 2021 1:59 am

your script is not indented, and have some syntax errors

get [find address="172.16.2.2/32"] enabled
this is impossible because "get enabled" do not exist, exist only "get disabled"

disabling what already is disabled do nothing,
enabling what already is enabled, only on interface cause problems, on ip not matter

Simply: (add { before and } after the script if tested on termial)
# this routine do not check if the same address is assigned for error (disabled) on multiple interfaces
# this routine do not check if the same address is assigned, with different subnet mask, to different interfaces
/ip address
:local findaddress [find where address="172.16.2.2/32"]
:if ([:ping 172.16.1.1 interface=ether2 interval=1 count=3] = 0) do={
    enable $findaddress
} else={
    disable $findaddress
}

about the get, on terminal:
# this routine CHECK if the same address is assigned for error (disabled) on multiple interfaces
# this routine do not check if the same address is assigned, with different subnet mask, to different interfaces
{
    /ip address
    :local findaddress [find where address="172.16.2.2/32"]
    :if ([:len $findaddress] = 1) do={
        :if ([get $findaddress disabled]) do={
          :put "The address is disabled!"
        } else={
          :put "The address is enabled!"
        }
    } else={
        :if ([:len $findaddress] = 0) do={
            :put "The address is not found!"
        } else={
            :put "The address is present on multiple interfaces!"
        }
    }
}

Who is online

Users browsing this forum: No registered users and 30 guests