Script problem with /ip address get X, where X interface not exist

Guys,
can you please help me with script?

I’m trying to achive “if interface doesn’t have valid IP log warning else do some action”

Script that I’m using is like:

:log info "debug 0";
:global iq [/ip address get 2 address];
log info "debug iq - $iq";

If interface 2 is disabled, or DHCP client on interface 2 is disabled the script will log only “debug 0” it will not get to “debug iq”

Similar behavior with this

:if ([:len [/ip address get 2 address]] < 9) do={
   :log warn "i am short";
}

The rest of the script is not executed.
When interface is running and DHCP client has IP it works. But I need to check for invalid situation.

Somehow I found the solution myself. So here is the code.

:global iq [/ip address find interface="non-existing-eth"]
:if ([:len $iq] != 0) do={
   :log info "exist";
} else={
   :log warn "Not exist";
}

It works this way…

  • It try to find interface “non-existing-eth” in /ip address
  • If it is not found IQ length is 0, so it will perform :log warn “Not exist”;
  • If it is found IQ length is not 0(usually 1), and you can work with the interface.