pear2 / Net_RouterOS

This package allows you to read and write information from a RouterOS host using the MikroTik RouterOS API protocol.
http://pear2.php.net/PEAR2_Net_RouterOS
241 stars 115 forks source link

Workflow question #66

Open drnasin opened 4 years ago

drnasin commented 4 years ago

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 commented 4 years ago

Also how do you remove the rule?

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

throws an error: Error when removing items

boenrobot commented 4 years ago

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')));
drnasin commented 4 years ago

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 commented 4 years ago

@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'));
                }
            }