Net_RouterOS icon indicating copy to clipboard operation
Net_RouterOS copied to clipboard

Workflow question

Open drnasin opened this issue 4 years ago • 4 comments

Hi guys. I am a full blooded PHP dev but I suck at mikrotik stuff. I have a task to do the following

"Add 2 rules. Make sure they are inserted (or moved afterwards) before a rule that has a comment "Block access to internet") I have no idea how to do that using UTIL class.

I can add rules. My problem is how to insert them or move them afterwards before the stated rule..

            $additionalRule = [];
            if($fw_rules_comment === 'Allow Teamviewer') {
                $additionalRule = $insertRule;
                $additionalRule['protocol'] = 'udp';
            }

            $insertRuleID = $util->add($insertRule);

            if(count($additionalRule)) {
                $additionalRuleID = $util->add($additionalRule);
            }

//make sure both rules are added or moved afterwards BEFORE the rule that has comment "Block access to internet"

Thank you!

drnasin avatar Aug 18 '20 12:08 drnasin

Also how do you remove the rule?

$util->remove(\PEAR2\Net\RouterOS\Query::where('comment', 'Allow Teamviewer'));

throws an error: Error when removing items

drnasin avatar Aug 18 '20 14:08 drnasin

RouterOS has a "place-before" property in menus with a move command. When you add, this inserts the new item before the target item.

To move an item afterwards, Util has a move() method that works in a similar fashion. See this part in the wiki page about it.

The remove of multiple items with a query should work in dev-master, but in 1.0.0b6, you can workaround it by wrapping the query in a find(), i.e.

$util->remove($util->find(\PEAR2\Net\RouterOS\Query::where('comment', 'Allow Teamviewer')));

boenrobot avatar Aug 19 '20 07:08 boenrobot

thank you @boenrobot

I did try that initially as well but I keep getting the same error message

$util->remove($util->find(\PEAR2\Net\RouterOS\Query::where('comment', 'Allow Teamviewer')));

drnasin avatar Aug 19 '20 11:08 drnasin

@boenrobot

Found another solution which works like a charm

           foreach ($util->getAll() as $rule) {
                if(intval($rule->getProperty('dst-port')) === 5938) {
                    $util->remove($rule->getProperty('.id'));
                }
            }

drnasin avatar Aug 19 '20 11:08 drnasin