Community discussions

MikroTik App
 
mbd777
just joined
Topic Author
Posts: 1
Joined: Tue Mar 12, 2024 10:29 pm

Network address instead of IP address

Tue Mar 12, 2024 10:33 pm

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.
 
tangent
Forum Guru
Forum Guru
Posts: 1406
Joined: Thu Jul 01, 2021 3:15 pm
Contact:

Re: Network address instead of IP address

Wed Mar 13, 2024 3:10 am

the current IP of ether1

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.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Network address instead of IP address

Wed Mar 13, 2024 10:25 am

: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
}
 
jaclaz
Long time Member
Long time Member
Posts: 668
Joined: Tue Oct 03, 2023 4:21 pm

Re: Network address instead of IP address

Wed Mar 13, 2024 11:19 am

To be fair, the snippet the OP posted is the same as the one published by Mikrotik here:
https://help.mikrotik.com/docs/display/ ... haschanged

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:
viewtopic.php?t=177121

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. :?
 
trkk
just joined
Posts: 17
Joined: Tue Mar 08, 2022 1:36 pm
Location: +09:00

Re: Network address instead of IP address

Wed Mar 13, 2024 2:36 pm

@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.
 
jaclaz
Long time Member
Long time Member
Posts: 668
Joined: Tue Oct 03, 2023 4:21 pm

Re: Network address instead of IP address

Wed Mar 13, 2024 2:57 pm

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.

Who is online

Users browsing this forum: No registered users and 6 guests