Network address instead of IP address

I’m 100% new to scripting so this will probably be something stupid I’m missing. But I’m trying to get the current IP of ether1 and add it to an address list. On the address list, I’m getting the network address (e.g. 192.168.200.0/24) instead of just the IP address (192.168.200.1). Am I pulling the data from the wrong value-name?

Here’s the code.

:global currentIP;
 :local newIP [/ip address get [find interface="ether1"] address];
 
:if ($newIP != $currentIP) do={
    :put [/ip firewall/address-list/add list=wan_ip address=$newIP dynamic=yes];
    :put "ip address $currentIP changed to $newIP";
    :set currentIP $newIP;
}

Any help is much appreciated.

What does that actually mean to you?

In the standard RouterOS configurations, ether1 is often a WAN uplink, placed into the “WAN” interface list, giving you little need to specify a particular IP. Is there a good reason you can’t simply say something like in-interface-list=WAN instead?

If not, it would help if you’d describe what you’re doing with this “wan_ip” list. A better solution may occur to someone once they have all the details. Without it, this feels like an X-Y problem.

:global currentIP
:local newIP [/ip address get [find interface="ether1"] address]
:set newIP [:pick $newIP 0 [:find $newIP "/" -1]]
 
:if ($newIP != $currentIP) do={
    :put [/ip firewall/address-list/add list=wan_ip address=$newIP dynamic=yes]
    :put "ip address $currentIP changed to $newIP"
    :set currentIP $newIP
}

To be fair, the snippet the OP posted is the same as the one published by Mikrotik here:
https://help.mikrotik.com/docs/display/ROS/Scripting+examples#Scriptingexamples-CheckifIPontheinterfacehaschanged

The snippet diamuxin posted is using commands to pick an element of an array.

This should mean that at a certain release the behaviour of RoS changed, from what I have found the behaviour was:

  1. if the interface has a single IP address the method of get worked
  2. if the interface has more than one IP address failed with an error (“invalid internal item number”)
    then it changed to:
  3. if the interface has more than one IP address return an empty value as
  4. what is returned is an array and so you have to pick the right element

Reference here:
http://forum.mikrotik.com/t/command-ip-address-get-not-working-anymore/150660/1

The get method should still work but only for interfaces with a single IP address, but since it is possible (and actually also common enough) to have more than one IP address on an interface, the “right” method is the “pick” one that should work in all cases.

What I don’t understand is why seemingly the “limited” command produced the network mask, it should have been either an error or an empty string. :confused:

@jaclaz
What @diamuxin wrote is,
“/ip/address/get $id address” returns string value like “192.168.88.1/24”,
so we have to strip netmask “/24” before adding it to /ip/firewall/address-list (if we don’t want to add network address to it).

And your indication is an another issue, certainly it’s a potential bug.
Of course it’s not good practice, but if the interface has exactly one address and we have confidence about it, it’s not a big matter, I think.

The OP stated that he got the network, specifically 192.168.200.0/24 instead of the expected 192.168.200.1, that’s why I was perplexed.
Now that is clear that the difference is that he got 192.168.200.1/24 while he wanted only the IP address without the /24, everything is back to normality.

Still, the difference between results of a same command depending on whether the target has one or more IP addresses - while maybe not a proper/serious bug - seems to be undocumented or poorly documented and the fact that the official help document uses the command that may fail without even a note about it doesn’t help the inexperienced people attempting to learn scripting.