Community discussions

 
Gesuino
just joined
Topic Author
Posts: 6
Joined: Mon Jan 21, 2019 5:28 pm

Subtract from get given IP

Mon Jan 21, 2019 5:39 pm

Hi, i have this problem:
/global IP [/ip neighbor get number=0 address]; :set "$IP" ($IP-1);
Script Error: cannot substract string from time interval
But it work if i set IP variable manually, there is a way to subtract an ip address given by print or get command?

Thanks
 
User avatar
eworm
Member
Member
Posts: 378
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Subtract from get given IP

Tue Jan 22, 2019 7:14 pm

Hi, i have this problem:
/global IP [/ip neighbor get number=0 address]; :set "$IP" ($IP-1);
Script Error: cannot substract string from time interval
But it work if i set IP variable manually, there is a way to subtract an ip address given by print or get command?

Thanks
How about this?
:global IP [ :toip ([ :toip [/ip neighbor get number=0 address ] ] - 1)];
Note that using index breaks if no print command was used before. In script you should use something like this if you want to work on the first neighbor:
:global IP [ :toip ([ :toip [/ip neighbor get ([ find where address~"." ]->0) address ] ] - 1)];
Manage RouterOS scripts and extend your devices' functionality: RouterOS Scripts
 
Gesuino
just joined
Topic Author
Posts: 6
Joined: Mon Jan 21, 2019 5:28 pm

Re: Subtract from get given IP

Fri Jul 26, 2019 8:53 pm

Thanks for your answer after many attempt i have found a solution. If you do an addiction to variable +0 is ok, then you can subtract without problems.
Here an example
:set $IP ($IP+0); :set $IP ($IP-1)
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1296
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: Subtract from get given IP

Fri Jul 26, 2019 10:32 pm

The reason why you see this, is that your IP is an string.
Adding 0 to it, it seems that RouterOS convert it to an IP, and you can subtraction.

Correct way to do it, is to convert it to an IP, then do the subtraction.

Cut and past this code and you will see what Is going on. You can try to swap :set $IP [:toip $IP] with :set $IP ($IP+0) and get the correct result, but its done in a wrong way.
{
:local IP "10.10.10.20"
:put $IP
:put [:typeof $IP]
:set $IP [:toip $IP]
:put [:typeof $IP]
:set $IP ($IP-1)
:put $IP
}
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 

Who is online

Users browsing this forum: No registered users and 2 guests