Community discussions

MikroTik App
 
User avatar
butche
Trainer
Trainer
Topic Author
Posts: 428
Joined: Fri May 28, 2004 6:14 pm
Location: Missouri, USA
Contact:

Using "find" to match partial comment

Mon Oct 24, 2005 6:59 pm

I would like to accomplish the following:

I have firewall rules as follows (example):

/ip firewall rule test
add src-address=10.10.10.10/32 action=drop comment="ACCEPT me"
add action=drop comment="DROP the rest"

What I would like to do is use find to allow me to manipulate only those items in the list that have "DROP" in the comment. I will not have the full text of the comments, as they are different, but ALL will have a common portion (i.e.: DROP or ACCEPT). I looked in the scripting manual, but could not get much beyond the fact that it seems possible. Any insights?
 
User avatar
Eugene
Forum Veteran
Forum Veteran
Posts: 986
Joined: Mon May 31, 2004 5:06 pm
Location: Cranfield, UK

Tue Oct 25, 2005 1:02 pm

[admin@MA_GW] > :put [:find "do not ACCEPT me" "ACCEPT"]
7
[admin@MA_GW] > :put [:find "do not ACCEPT me" "not found"]

[admin@MA_GW] >
:find string substring searches for the first inclusion of substring in the string and returns position at which a match has occured.

Hope this helps.
 
User avatar
butche
Trainer
Trainer
Topic Author
Posts: 428
Joined: Fri May 28, 2004 6:14 pm
Location: Missouri, USA
Contact:

Tue Oct 25, 2005 5:29 pm

[admin@MA_GW] > :put [:find "do not ACCEPT me" "ACCEPT"]
7
[admin@MA_GW] > :put [:find "do not ACCEPT me" "not found"]

[admin@MA_GW] >
Ok...any idea how I get find to parse the list of firewall rules comments?

In your example, you have a known string. Something like this (this doesn't work...)
/ip firewall filter set [:find <comment=UNKNOWN> "SOMETHING"] action=allow
In other words, I want to look through all rules for the comments that contain "SOMETHING" and do something with those rules.
 
User avatar
Eugene
Forum Veteran
Forum Veteran
Posts: 986
Joined: Mon May 31, 2004 5:06 pm
Location: Cranfield, UK

Tue Oct 25, 2005 6:05 pm

Well, nothing easier than that:
:foreach i in [/ip firewall filter find] do={
  :if ("." . [:find [/ip firewall filter get $i comment] "SOMETHING"] . "."!="..") do={
    /ip firewall filter set $i action=accept
  }
}
:D
 
User avatar
butche
Trainer
Trainer
Topic Author
Posts: 428
Joined: Fri May 28, 2004 6:14 pm
Location: Missouri, USA
Contact:

Tue Oct 25, 2005 6:22 pm

Well, nothing easier than that:
:foreach i in [/ip firewall filter find] do={
  :if ("." . [:find [/ip firewall filter get $i comment] "SOMETHING"] . "."!="..") do={
    /ip firewall filter set $i action=accept
  }
}
:D
You da' man! :P
 
kiwistag
just joined
Posts: 16
Joined: Mon Jun 24, 2013 12:53 am
Location: New Zealand

Re: Using "find" to match partial comment

Mon Dec 03, 2018 3:59 am

Hi, how would this work if you wanted a MAC address from say a DHCP lease? That way it'd be possible to run WOL to a classroom/office area based on computer name.
:foreach i in [/ip dhcp-server lease mac find] do={
  :if ("." . [:find [/ip dhcp-server lease get $i active-host-name] "Student-Class2"] . "."!="..") do={
  *******
    /tool wol mac=$j interface=LAN
  }
}
I think I need another search call to first get the MAC address off the list then pass it to the final command. Still quite new to it so please excuse the ignorance to the extra command needed (*****)
 
User avatar
sebastia
Forum Guru
Forum Guru
Posts: 1782
Joined: Tue Oct 12, 2010 3:23 am
Location: Antwerp, BE

Re: Using "find" to match partial comment

Mon Dec 03, 2018 12:14 pm

api allow to search on number of parameters, including comment. The match can be full or partial. Ex:

print where comment~"est_rel"

Who is online

Users browsing this forum: No registered users and 35 guests