Close Connection

Hi
I can connect to routerOS api via pear2

$client = new RouterOS\Client(‘host’, ‘user’, ‘pass’, ‘port’);

What is command for close connection now?
Api user in users table still logged in?

Simply unsetting the $client variable with unset(), or eliminating it from any scope* should disconnect. It should even implicitly send a “/quit” command, as some older RouterOS versions and some RouterBoards are known to otherwise keep the connection hanging.

The destructor should also be called at the end of the PHP script though. If your script is over, and the connection is still hanging, there’s something else going on. So far, I know of only 2 cases where this can happen:

  1. sending over 2GiB in a single word. This causes the remaining words not to be sent, and the “/quit” being merged with the last request, thus being ignored. Combined with the aforementioned older RouterOS versions and RouterBoards, the connection is left hanging. Solution: Make sure you’re not sending data that large.
  2. (the more likely case) A non-recoverable fatal error or parse error that has occurred after the connection is created. Destructors are not called in such a case. Solution: Either unset() explicitly before there’s the potential for that error, or better yet - check PHP’s error log for fatal/parse errors, and fix them.
  • e.g. If you connect within a function/method, when that function/method is finished, variables in its scope are destroyed, unless you’ve passed them to a global variable or something… thus a disconnect with a “/quit” being sent too.

Thank you for using PEAR2_Net_RouterOS.

Thank you for your reply.

This is my api commands:

<?php use PEAR2\Net\RouterOS; require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar'; $username = strtolower($rw['pay_from']); if ($rw['memo'] == "یک ماه 128 کیلوبیت نامحدود") {$addcredit = "30d"; $groupname = "128";} else if ($rw['memo'] == "یک ماه 256 کیلوبیت نامحدود") {$addcredit = "30d"; $groupname = "256";} else if ($rw['memo'] == "یک ماه 512 کیلوبیت نامحدود") {$addcredit = "30d"; $groupname = "512";} else if ($rw['memo'] == "یک ماه 1 مگابیت نامحدود") {$addcredit = "30d"; $groupname = "1024";} else if ($rw['memo'] == "یک ماه 2 مگابیت نامحدود") {$addcredit = "30d"; $groupname = "2048";} else if ($rw['memo'] == "یک ماه 3 مگابیت نامحدود") {$addcredit = "30d"; $groupname = "3072";} else if ($rw['memo'] == "سه ماه 128 کیلوبیت نامحدود") {$addcredit = "90d"; $groupname = "128";} else if ($rw['memo'] == "سه ماه 256 کیلوبیت نامحدود") {$addcredit = "90d"; $groupname = "256";} else if ($rw['memo'] == "سه ماه 512 کیلوبیت نامحدود") {$addcredit = "90d"; $groupname = "512";} else if ($rw['memo'] == "سه ماه 1 مگابیت نامحدود") {$addcredit = "90d"; $groupname = "1024";} else if ($rw['memo'] == "سه ماه 2 مگابیت نامحدود") {$addcredit = "90d"; $groupname = "2048";} else if ($rw['memo'] == "سه ماه 3 مگابیت نامحدود") {$addcredit = "90d"; $groupname = "3072";} else if ($rw['memo'] == "شش ماه 128 کیلوبیت نامحدود") {$addcredit = "180d"; $groupname = "128";} else if ($rw['memo'] == "شش ماه 256 کیلوبیت نامحدود") {$addcredit = "180d"; $groupname = "256";} else if ($rw['memo'] == "شش ماه 512 کیلوبیت نامحدود") {$addcredit = "180d"; $groupname = "512";} else if ($rw['memo'] == "شش ماه 1 مگابیت نامحدود") {$addcredit = "180d"; $groupname = "1024";} else if ($rw['memo'] == "شش ماه 2 مگابیت نامحدود") {$addcredit = "180d"; $groupname = "2048";} else if ($rw['memo'] == "شش ماه 3 مگابیت نامحدود") {$addcredit = "180d"; $groupname = "3072";} else if ($rw['memo'] == "یکسال 128 کیلوبیت نامحدود") {$addcredit = "365d"; $groupname = "128";} else if ($rw['memo'] == "یکسال 256 کیلوبیت نامحدود") {$addcredit = "365d"; $groupname = "256";} else if ($rw['memo'] == "یکسال 512 کیلوبیت نامحدود") {$addcredit = "365d"; $groupname = "512";} else if ($rw['memo'] == "یکسال 1 مگابیت نامحدود") {$addcredit = "365d"; $groupname = "1024";} else if ($rw['memo'] == "یکسال 2 مگابیت نامحدود") {$addcredit = "365d"; $groupname = "2048";} else if ($rw['memo'] == "یکسال 3 مگابیت نامحدود") {$addcredit = "365d"; $groupname = "3072";} else if ($rw['memo'] == "یکسال + 2 ماه رایگان 128 کیلوبیت نامحدود") {$addcredit = "420d"; $groupname = "128";} else if ($rw['memo'] == "یکسال + 2 ماه رایگان 256 کیلوبیت نامحدود") {$addcredit = "420d"; $groupname = "256";} else if ($rw['memo'] == "یکسال + 2 ماه رایگان 512 کیلوبیت نامحدود") {$addcredit = "420d"; $groupname = "512";} else if ($rw['memo'] == "یکسال + 2 ماه رایگان 1 مگابیت نامحدود") {$addcredit = "420d"; $groupname = "1024";} else if ($rw['memo'] == "یکسال + 2 ماه رایگان 2 مگابیت نامحدود") {$addcredit = "420d"; $groupname = "2048";} else if ($rw['memo'] == "یکسال + 2 ماه رایگان 3 مگابیت نامحدود") {$addcredit = "420d"; $groupname = "3072";} $client = new RouterOS\Client('***', '***', '***', '***'); $setRequest = new RouterOS\Request('/tool user-manager user set'); $client($setRequest ->setArgument('numbers', $username) ->setArgument('add-credit', $addcredit) ->setArgument('group-name', $groupname) ); ?>

And this is active users:

WHEN NAME ADDRESS VIA

0 apr/05/2014 11:20:59 admin *** api
1 apr/05/2014 15:04:14 admin *** api
2 apr/05/2014 18:18:47 admin *** api

Please post that command for closing sessions.
Thank you

Before the “?>”:

unset($client);

That is all.


Note that this will not close your existing connections. It will only make sure new connections don’t stick around. Sadly, the only way to clear up old connections is to reboot your router, since your web server has already closed the connection, and can’t somehow “resume” it.

This code not worked :frowning:

);
unset($client);
?>

The session still logged in..

Like I said - this will NOT close your existing connections.

You must reboot the router to clear those.

I know.
It means new api connection not removed with this code.
The new connection added to user active list.

 #   WHEN                 NAME                       VIA           
 0   apr/05/2014 11:21:00 admin                  api           
 1   apr/05/2014 15:04:14 admin                  api           
 2   apr/05/2014 18:18:47 admin                  api           
 3   apr/06/2014 16:27:25 admin                  api             
 5   apr/06/2014 16:29:56 admin                  api           
 6   apr/06/2014 16:31:34 admin                  api

New sessions are not removed? That’s odd…

Try to explicitly send a “/quit” command at the end, i.e.

$client($setRequest
->setArgument('numbers', $username)
->setArgument('add-credit', $addcredit)
->setArgument('group-name', $groupname)
);
$client(new RouterOS\Request('/quit'));
 

Also, please check if there’s anything in PHP’s error logs. It should go without saying that you should enable error logging if you haven’t.

Still not working.
Winbox log only say:
user admin logged in from *** via api

What RouterOS version are you using? PHP version?

(I’ll have to try and duplicate this, as with my setup, I don’t seem to be able to…)

RouterOS 4.17
PHP Version 5.3.28

Yep… It’s a RouterOS bug. I can duplicate it with a VM.

I can’t think of any solution here though… Other than to upgrade at least to 5.. The 4. series is already not supported by MikroTik.