Community discussions

MikroTik App
 
Fulmo
just joined
Topic Author
Posts: 15
Joined: Mon Feb 22, 2021 12:32 pm

Ip search by address and enable

Tue Apr 27, 2021 11:59 am

Hello there!

I need to find ip addresses where :
The ip address is need to be:
10.100.0.* or 10.100.*.1 and if this statement is true than i need to run something.
my try:
if ([:len [/ip address find where address=10.100.0.* disabled=no]]>0
or
[:len [/ip address find where address=10.100.*.1 disabled=no]]>0) do={};
probably it doesn't work for me.
So i tried with regex but i don't know how to use it...
if ([:len [/ip address find where address=10.100.0."^[0-9]{1,3}" disabled=no]]>0
or
[:len [/ip address find where address=10.100.*.1 disabled=no]]>0) do={};
but unfortunately that's give me back 0 too so idk what can i do.
So give me a helping hand please :)
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Ip search by address and enable

Tue Apr 27, 2021 12:35 pm

There are several error in your logic.

You search for a range of IP, but you does not tell where the disable command should look.

Also what would you like to happen if it find three IP and 2 are enabled a 1 is disabled?

To use wildcard, you can use:
:put [/ip address find where address~"10.100.0.*"]
To test 1 specific IP, if its found and enabled:
{
/ip address 
:local IPID [find where address~"10.100.21.1"]
:if ([:len $IPID]=0) do={
	:put "ip not found"
} else={
	:local IPStatus [get $IPID disabled]
	:if ([:len $IPID]>0 && $IPStatus=false) do={
		:put "ip found and enabled"
	} else={
		:put "ip found and disabled"
	}
}
}
You can test more IP, but then you need to use a loop.
 
Fulmo
just joined
Topic Author
Posts: 15
Joined: Mon Feb 22, 2021 12:32 pm

Re: Ip search by address and enable

Tue Apr 27, 2021 1:11 pm

i want to exchange ospf instence router id from 0.0.0.0 to a 10.100. something address.
But:
i dont want to exchange the address to a severel ways, like if the address is something like this:

addresses with network 10.100.0.0/24
or
address like 10.100.*.1

so i need to unselect these addresses, in different routers so i cant select only one ip and check it, because i don't know the complete ip

And i think if i try to put an address into a variable and its doesnt exists than the script got an error and break the script
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Ip search by address and enable

Tue Apr 27, 2021 1:46 pm

addresses with network 10.100.0.0/24
or
address like 10.100.*.1
If these ranges only contain one IP this should work,
{
/ip address 
:local IPID [find where address~"10.100.0.*"]
:if ([:len $IPID]=0) do={
	:put "ip not found"
} else={
	:local IPStatus [get $IPID disabled]
	:if ([:len $IPID]>0 && $IPStatus=false) do={
		:put "ip found and enabled"
	} else={
		:put "ip found and disabled"
	}
}
}
But since I do not have any clue about OSPF, I am not sure how to implement this.
 
Fulmo
just joined
Topic Author
Posts: 15
Joined: Mon Feb 22, 2021 12:32 pm

Re: Ip search by address and enable

Tue Apr 27, 2021 3:03 pm

probably i can't circumscribes clearly what i want exactly so :D i end this part of my question and because i don't want to make another one i write my new question to here.

if this symbol ~ mean the "something like" than what is the it's not like as
i thinked its will be the !~ but that's give me back 0 collums.
And i know its again a rly rly bad question, but thats me.. i cant make any good question
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Ip search by address and enable

Tue Apr 27, 2021 4:20 pm

~ Does contain some.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Ip search by address and enable

Wed Apr 28, 2021 10:59 pm

if this symbol ~ mean the "something like" than what is the it's not like as
i thinked its will be the !~ but that's give me back 0 collums.

viewtopic.php?f=9&t=174837&p=855155#p855172

:if ("abtestcd"~"test") do={
 :put "test found"
} else={
 :put "test not found"
}


:if (!("abtestcd"~"test")) do={
 :put "test not found"
} else={
 :put "test found"
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Ip search by address and enable

Wed Apr 28, 2021 11:14 pm


To use wildcard, you can use:
:put [/ip address find where address~"10.100.0.*"]

sorry, but * = zero or more of previous

10.100.0.* can match (ignoring previous dot) 10.100.0, 10.100.0/, 10.100.0//, .... 10.100.0aaaa ... 10.100.011111111111111111111111111111111111111 ... etc.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Ip search by address and enable

Wed Apr 28, 2021 11:37 pm

Go easy....

See my example:

it do not consider valid IP the IP or ranges with prefix like 10.100.12.15/32 or 10.100.15.0/24
:global testStart 10.100.10.0
:global testEnd 10.100.99.0
:global compare 10.100.55.55

:if ([:typeof $compare] = "ip") do={
  :if ( ([:toip $compare] >= $testStart) && ([:toip $compare] <= $testEnd) ) do={
   :put "IP on range"
  } else={
   :put "IP out of range"
  }
}
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Ip search by address and enable

Thu Apr 29, 2021 8:30 am

@rextended

You are correct. Since it regex it should be like this for 10.100.0.*:
:put [/ip address find where address~"10\\.100\\.0\\.[0-9]+"]
Or even more correct
:put [/ip address find where address~"10\\.100\\.0\\.[0-9]{1,3}"]
And for 10.100.*.1 :
:put [/ip address find where address~"10\\.100\\[0-9]{1,3}\\.1"]
PS In your example I would have used local variable and not global, if the variable are not needed outside the script.
 
Fulmo
just joined
Topic Author
Posts: 15
Joined: Mon Feb 22, 2021 12:32 pm

Re: Ip search by address and enable

Thu Apr 29, 2021 9:31 am

Probably i solved it. Probably i wanted the else statement because if the if statement is true than it's probably not good for me. But than i used something like this:
if ([:len \\\r\
    \n[/ip address find where address~\"^10\\\\.100\\\\.([1-9]{1,3}|[0-9]{2,3})\\\\.\
    ([02-9]{1,3}|[0-9]{2,})\" disabled=no]]>0) \\\r\
    \ndo={
    
So i'm sry to steal your time.

Who is online

Users browsing this forum: m4rk3J and 17 guests