Skip to content

Instantly share code, notes, and snippets.

@marsalans
Forked from Owkkuri/router.php
Created February 24, 2019 20:22
Show Gist options
  • Save marsalans/019a5d114d9efcc2a74793a1d3527923 to your computer and use it in GitHub Desktop.
Save marsalans/019a5d114d9efcc2a74793a1d3527923 to your computer and use it in GitHub Desktop.
<?php
namespace Your\Namespace;
use Exception;
use PEAR2\Net\RouterOS\Client as rosClient;
use PEAR2\Net\RouterOS\Request;
use PEAR2\Net\RouterOS\Response;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class RouterCommand extends Command
{
/** @var rosClient */
private $RouterOSclient;
private $keys;
private $counter;
private $interfaces;
protected function configure()
{
$this
->setName('router:run')
->setDescription('Collect and dump router interface stats');
}
private function setup()
{
$host = 'your host';
$user = 'your user';
$pass = 'your password';
try {
$this->RouterOSclient = new rosClient($host, $user, $pass);
} catch (Exception $e) {
die('/');
}
$this->keys = array(
'rx-bits-per-second',
'tx-bits-per-second',
'rx-packets-per-second',
'tx-packets-per-second',
);
$this->interfaces = [
'ether1',
'ether2',
'ether3',
'ether4',
'ether5',
'wlan1',
'wlan2',
'bridge',
];
$this->counter = 0;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->setup();
$request = new Request('/interface/monitor-traffic interface=' . implode(',', $this->interfaces));
$request->setTag('stats');
$this->RouterOSclient->sendAsync($request, array($this, 'responseHandler'));
$this->RouterOSclient->loop(30);
}
public function responseHandler(Response $response)
{
foreach ($response as $key => $value) {
if (!in_array($key, $this->keys)) {
continue;
}
var_dump($key, $value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment