Community discussions

MikroTik App
 
Nova
newbie
Topic Author
Posts: 25
Joined: Mon Aug 04, 2014 3:44 pm
Location: Spain // Germany

API + C# + Firewall Filter

Wed Jul 01, 2015 11:08 am

Good day,

One of the function of the program I`m doing is to add an specific rule X after another specific rule Y. That is easy when all the firewall rules are static and Y is always the same number. For example the rule Y is always in number 37 so I simply add:
mikrotik.Send("/ip/firewall/filter/add");
                mikrotik.Send("=place-before=38");....
It works, but in "real life" the rules are never static, so i have to find this rule and try to add it after. I've read and I've seen that the find function doesn't exist in API, instead we should use print or getall, but it is kinda complicated for me.

This is what I've tried without success:
mikrotik.Send("/ip/firewall/filter/print");
                mikrotik.Send("?dst-address=x.x.x.x/x");
                mikrotik.Send("=.proplist=.id=" + id, true);
                mikrotik.Send("/ip/firewall/filter/add");
                mikrotik.Send("=place-before=" + id);
also:
mikrotik.Send("/ip/firewall/filter/print");
                mikrotik.Send("?dst-address=x.x.x.x/x", true);
                foreach (var item in mikrotik.Read())
                {
                    id = item;
                }
                mikrotik.Send("=place-before=" + id);
                
I dont' even know if place-before works with the id or what, is there a way to get the number of the rule Y so I can use the place-before?

Wiki doesn't help much:
place-before - places a new item before an existing item with specified position. Thus, you do not need to use the move command after adding an item to the list
I'm kinda lost, if anyone could point me the direction would be great.

Thanks


EDIT: I 've figured it out, I leave this here so it maybe can help someone like me. This is the solution I've found, I had to split and get the id from the string:
mikrotik.Send("/ip/firewall/filter/print");
                mikrotik.Send("?dst-address=x.x.x.x/x");
                mikrotik.Send("=.proplist=.id",true);
                foreach (var item in mikrotik.Read())
                {
                    id += item;
                }
                //Id = !re=.id=*25!done
                string sub = id.Substring(id.IndexOf("*"), id.LastIndexOf("!") - id.IndexOf("*"));
                
                //sub = *25
                
                mikrotik.Send("/ip/firewall/filter/add");
                mikrotik.Send("=place-before=" + sub);

Who is online

Users browsing this forum: No registered users and 3 guests