I have some stuff connected with integrating billing with Mikrotik routers. Write an email to me.Hello,
I need to help Connect Mikrotik to PHP
I have billing system for users but I can not automatic add, disable, enable or edit users in mikrotik from php
can anyone help me?
<?php
require('mikrotik.php');
$API = new routeros_api();
$API->debug = true;
if ($API->connect('123.123.123.123', 'admin', 'root')) {
$API->write('ip firewall filter add chain=input src-address=13.13.13.13 action=accept disabled=yes');
$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);
print_r($ARRAY);
$API->disconnect();
}
Connection attempt #1 to 123.123.123.123:8728... <<< [6] /login >>> [5/5 bytes read. >>> [5, 39] !done >>> [37/37 bytes read. >>> [37, 1] =ret=6484868b370e2f2c381b06f161e5d226 <<< [6] /login <<< [11] =name=admin <<< [44] =response=0023ad4ee121dab3e5d0e23457ce4dd210 >>> [5/5 bytes read. >>> [5, 1] !done Connected... <<< [85] ip firewall filter add chain=input src-address=13.13.13.13 action=accept disabled=yes >>> [5/5 bytes read. >>> [5, 40] !trap >>> [31/31 bytes read. >>> [31, 8] =message=no such command prefix >>> [5/5 bytes read. >>> [5, 1] !done Array ( [!trap] => Array ( [0] => Array ( [message] => no such command prefix ) ) ) Disconnected...
Take a look at the examples in the wiki. API command syntax is different than cli. The above command would be written something like this:Code: Select all<?php require('mikrotik.php'); $API = new routeros_api(); $API->debug = true; if ($API->connect('123.123.123.123', 'admin', 'root')) { $API->write('ip firewall filter add chain=input src-address=13.13.13.13 action=accept disabled=yes'); $READ = $API->read(false); $ARRAY = $API->parse_response($READ); print_r($ARRAY); $API->disconnect(); }
$API->write('/ip/firewall/filter/add', false); //this is the basic command; false says the there are more lines to the command.
$API->write('=chain=input',false); // this is another part of the command
$API->write('=src-address=13.13.13.13',false); // more
$API->write('=action=accept', false); //more
$API->write('=disabled=yes'); //all done;
from php I think script is:ip firewall filter set action=drop numbers=2
$API->write('/ip/firewall/filter/set', false);
$API->write("=action=drop",false);
$API->write("=numbers=2");
. <<< [23] /ip/firewall/filter/set <<< [13] =disabled=yes <<< [10] =numbers=2 >>> [5/5 bytes read. >>> [5, 42] !trap >>> [11/11 bytes read. >>> [11, 30] =category=0 >>> [21/21 bytes read. >>> [21, 8] =message=no such item >>> [5/5 bytes read. >>> [5, 1] !done Array ( [!trap] => Array ( [0] => Array ( [category] => 0 [message] => no such item ) ) ) Disconnected
"numbers" don't work in api, afaict. You have to find the ".id" of the filter rule first, then use that to change the rule.ip firewall filter set action=drop numbers=2
from php I think script is:Code: Select all$API->write('/ip/firewall/filter/set', false); $API->write("=action=drop",false); $API->write("=numbers=2");
$API->write("/ip/firewall/filter/print");
$API->write("?src-address=13.13.13.13");
$API->write('/ip/firewall/filter/set', false);
$API->write("=.id=<the id goes here>", false);
$API->write("=action=drop");
$API->write('/ip/firewall/filter/add',false);
$API->write('=chain=forward',false);
$API->write("=src-address=$ip",false);
$API->write('=action=accept',false);
$API->write('=comment='.$abonenti.'',false);
$API->write("=src-mac-address=$maci",false);
$API->write("=place-before=.0",false);
$API->write('=disabled=no');
*2DA
*2
*A
/ip/firewall/print
=.proplist=.id
=.id=*A
=.id=*B
=.id=*C
$API->write('/ip/firewall/filter/print',false);
$API->write('?comment=000009');
$API->write('/ip/firewall/filter/remove',false);
$API->write('=.id=*HEXNUM');
Can u share billing system ? If yes send me pm with linkHello,
I need to help Connect Mikrotik to PHP
I have billing system for users but I can not automatic add, disable, enable or edit users in mikrotik from php
can anyone help me?
now its only beta versionCan u share billing system ? If yes send me pm with link
$API->write('/ip/firewall/filter/edit',false);
$API->write("=.id=$id",false);
$API->write("=src-address=$ip",false);
$API->write("=src-mac-address=$maci");
I cant understand what is mistakeConnection attempt #1 to 123.123.123.123:8728... <<< [6] /login >>> [5/5 bytes read. >>> [5, 39] !done >>> [37/37 bytes read. >>> [37, 1] =ret=a58048c26a26d61fde722fce4750f34f <<< [6] /login <<< [11] =name=admin <<< [44] =response=00b54e6533572cc9a3992c023e24fddfd8 >>> [5/5 bytes read. >>> [5, 1] !done Connected... <<< [24] /ip/firewall/filter/edit <<< [10] =.id=00001 <<< [26] =src-address=12.34.12.34 <<< [17] =src-mac-address= >>> [5/5 bytes read. >>> [5, 35] !trap >>> [26/26 bytes read. >>> [26, 8] =message=unknown parameter >>> [5/5 bytes read. >>> [5, 1] !done Array ( [!trap] => Array ( [0] => Array ( [message] => unknown parameter ) ) ) Disconnected...
this is the problem. attribute .id value is hexadecimal number without leading zeros prefixed with *<<< [10] =.id=00001
$API->write('/ip/firewall/filter/edit',false);
$API->write("=.id=1234",false);
$API->write("=src-address=$ip",false);
$API->write("=src-mac-address=$mac");
$API->write('/ip/firewall/filter/edit',false);
$API->write("=.id=*" . dechex(1234), false);
$API->write("=src-address=$ip",false);
$API->write("=src-mac-address=$mac");
$API->write('/ip/firewall/filter/edit',false);
$API->write("=.id=*" . dechex(3015), false);
$API->write("=src-address=$ip",false);
$API->write("=src-mac-address=$maci");
Connection attempt #1 to 123.1.1.1:8728... <<< [6] /login >>> [5/5 bytes read. >>> [5, 39] !done >>> [37/37 bytes read. >>> [37, 1] =ret=ebab08056bea7de07c8ced7c4853c44c <<< [6] /login <<< [11] =name=admin <<< [44] =response=00c4a05c93495a60d067f7e8520ff3659d >>> [5/5 bytes read. >>> [5, 1] !done Connected... <<< [24] /ip/firewall/filter/edit <<< [9] =.id=*bc7 <<< [26] =src-address=172.31.247.40 <<< [17] =src-mac-address= >>> [5/5 bytes read. >>> [5, 35] !trap >>> [26/26 bytes read. >>> [26, 8] =message=unknown parameter >>> [5/5 bytes read. >>> [5, 1] !done Array ( [!trap] => Array ( [0] => Array ( [message] => unknown parameter ) ) ) Disconnected...
<?php
namespace PEAR2\Net\RouterOS;//This must remain at the top of your PHP file!
require_once 'PEAR2/Net/RouterOS/Autoload.php';
//The rest of your PHP script here... particularly important is that you define $ip and $mac
$client = new Client('123.123.123.123', 'admin', 'root'));
//The key bit... getting the correct ID by printing it
$id = $client->sendSync(new Request('/ip/firewall/filter/print .proplist=.id', null, Query::where('comment', '3015')))->getArgument('.id');
$setRequest = new Request('/ip/firewall/filter/set');
$setRequest->setArgument('numbers', $id);
$setRequest->setArgument('src-address', $ip);
$setRequest->setArgument('src-mac-address', $mac);
$client->sendSync($setRequest);
//And you're done.
require('routeros_api.class.php'); // RouterOS API class
$API = new routeros_api();
$API->debug = true;
include "router_config.php"; // router username and password
{
$abonentiAPI = $API->connect(new Request('/ip/firewall/filter/print .proplist=.id', null, Query::where('comment', "$abonenti")))->getArgument('.id');
$setRequest = new Request('/ip/firewall/filter/set');
$setRequest->setArgument('numbers', $abonentiAPI);
$setRequest->setArgument('src-address', $ip);
$setRequest->setArgument('src-mac-address', $maci);
$client->sendSync($setRequest);
}
$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);
print_r($ARRAY);
$API->disconnect();
require('routeros_api.class.php'); // RouterOS API class
$API = new routeros_api();
$API->debug = true;
include "router_config.php"; // router username and password
<?php
namespace PEAR2\Net\RouterOS;//This must remain at the top of your PHP file!
require_once 'PEAR2/Net/RouterOS/Autoload.php';
Fatal error: Uncaught exception 'LogicException' with message 'Function '__NAMESPACE__\autoload' not found' in /home/fastnetg/public_html/baza/api/PEAR2/Net/RouterOS/Autoload.php:56 Stack trace: #0 /home/f/public_html/test/api/PEAR2/Net/RouterOS/Autoload.php(56): spl_autoload_register() #1 /home/f/public_html/test/api/callback-and-loop.php(2): require_once('/home/fastnetg/...') #2 {main} thrown in /home/f/public_html/test/api/PEAR2/Net/RouterOS/Autoload.php on line 56
Quote the key, i.e.How can I show the $ARRAY[0][.id] value? The .id make error because the "." character is invalid.
$ARRAY[0]['.id']
Thank you, I'll try PHP 5.3You need to have PHP 5.3.0 or later. The error you first saw can only mean that you have an earlier version (e.g. 5.2.*).
If you're doing this on your own machine, upgrade your PHP. If you're using a host, ask your host to upgrade the PHP version for your site.
Don't forget to add back the "namespace PEAR2\Net\RouterOS;" line once that is done.
Thank you your answer! Your say right, I had to make a new topic. Sorry for all users!Quote the key, i.e.How can I show the $ARRAY[0][.id] value? The .id make error because the "." character is invalid.This is a common requirement for ALL strings (i.e. text) in PHP. You can use either single quotes (apostrophes) as above, or double quotes, although I'd personally reccomend single, because they are interpreted literally (as opposed to double quoted strings, with which variables are expanded). That would in turn save you some trouble later on.Code: Select all$ARRAY[0]['.id']
BTW, next time, you might want to make a new topic instead of snatching another user's topic... your issues are not really related anyway.
<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';
$client = new Client('1.2.3.4', 'admin', '123456');
$addRequest = new Request('/ip/firewall/filter/add');
$addRequest->setArgument('chain', 'forward');
$addRequest->setArgument('src-address', "$ip");
$addRequest->setArgument('action', 'accept');
$addRequest->setArgument('comment', "$abonenti");
$addRequest->setArgument('src-mac-address', "$mac");
$addRequest->setArgument('disabled', 'no');
$addRequest->setTag('arp1');
$client->sendAsync($addRequest);
$addRequest = new Request('/ip/firewall/filter/add');
$addRequest->setArgument('chain', 'forward');
$addRequest->setArgument('dst-address', "$ip");
$addRequest->setArgument('action', 'accept');
$addRequest->setArgument('comment', '$abonenti - 2');
$addRequest->setArgument('disabled', 'no');
$addRequest->setTag('arp2');
$client->sendAsync($addRequest);
$client->loop();
?>
$printRequest = new Request('/ip/firewall/filter/print');
$printRequest->setArgument('.proplist', '.id');
$destinationId = $client->sendSync($printRequest)->getArgument('.id');
$printRequest->setQuery(Query::where('comment', $abonenti));
$sourceId = $client->sendSync($printRequest)->getArgument('.id');
$moveRequest = new Request('/ip/firewall/filter/move');
$moveRequest->setArgument('numbers', $sourceId);
$moveRequest->setArgument('destination', $destinationId);
$client->sendSync($moveRequest);
$printRequest = new Request('/ip/firewall/filter/print');
$printRequest->setArgument('.proplist', '.id');
$printRequest->setQuery(Query::where('comment', $abonenti));
$id = $client->sendSync($printRequest)->getArgument('.id');
$setRequest = new Request('/ip/firewall/filter/set');
$setRequest
->setArgument('numbers', $id)
->setArgument('src-address', $ip)
->setArgument('src-mac-address', $mac)
->setArgument('comment', $newAbonenti);
$client->sendSync($setRequest);
$resultpackupdate = mysql_query("SELECT * FROM packets WHERE router='".$row['router']."' and packetid='".$packetid."'");
while($rowpackupdate = mysql_fetch_assoc($resultpackupdate))
{
$printRequest = new Request('/ip/firewall/filter/print');
$printRequest->setArgument('.proplist', '.id');
$printRequest->setQuery(Query::where('comment', $abonenti));
$id = $client->sendSync($printRequest)->getArgument('.id');
$setRequest = new Request('/queue/simple/set');
$setRequest
->setArgument('numbers', $id)
->setArgument('max-limit', $rowpackupdate["local"])
->setArgument('comment', $abonenti . ' L');
$client->sendSync($setRequest);
$setRequest = new Request('/queue/simple/set');
$setRequest
->setArgument('numbers', $id)
->setArgument('max-limit', $rowpackupdate["global"])
->setArgument('comment', $abonenti . ' G');
$client->sendSync($setRequest);
}
$printRequest = new Request('/queue/simple/print');
$printRequest->setArgument('.proplist', '.id');
$setRequest = new Request('/queue/simple/set');
$resultpackupdate = mysql_query("SELECT * FROM packets WHERE router='".$row['router']."' and packetid='".$packetid."'");
while($rowpackupdate = mysql_fetch_assoc($resultpackupdate))
{
$printRequest->setQuery(Query::where('comment', $abonenti . ' L'));
$id = $client->sendSync($printRequest)->getArgument('.id');
$setRequest
->setArgument('numbers', $id)
->setArgument('max-limit', $rowpackupdate["local"]);
$client->sendSync($setRequest);
$printRequest->setQuery(Query::where('comment', $abonenti . ' G'));
$id = $client->sendSync($printRequest)->getArgument('.id');
$setRequest
->setArgument('numbers', $id)
->setArgument('max-limit', $rowpackupdate["global"]);
$client->sendSync($setRequest);
}
$printRequest = new Request('/queue/simple/print');
$printRequest->setArgument('.proplist', '.id');
$setRequest = new Request('/queue/simple/set');
$lAbonentiQuery = Query::where('name', $abonenti . ' L');
$gAbonentiQuery = Query::where('name', $abonenti . ' G');
$resultpackupdate = mysql_query("SELECT * FROM packets WHERE router='".$row['router']."' and packetid='".$packetid."'");
while($rowpackupdate = mysql_fetch_assoc($resultpackupdate))
{
$printRequest->setQuery($lAbonentiQuery);
$id = $client->sendSync($printRequest)->getArgument('.id');
$setRequest
->setArgument('numbers', $id)
->setArgument('max-limit', $rowpackupdate["local"]);
$client->sendSync($setRequest);
$printRequest->setQuery($gAbonentiQuery);
$id = $client->sendSync($printRequest)->getArgument('.id');
$setRequest
->setArgument('numbers', $id)
->setArgument('max-limit', $rowpackupdate["global"]);
$client->sendSync($setRequest);
}
CREATE TABLE IF NOT EXISTS `stats` (
`time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`rx-bits-per-second` INT UNSIGNED NOT NULL ,
`tx-bits-per-second` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`time`) )
ENGINE = InnoDB;
<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';
try {
$client = new Client('1.2.3.4', 'admin', '123456');
$mysqli = new \mysqli('localhost', 'root', '', 'db');
} catch (\Exception $e) {
die('Connection error: ' . $e);
}
$insertQuery = $mysqli->prepare('INSERT INTO `stats` (`rx-bits-per-second`, `tx-bits-per-second`) VALUES (?, ?)');
$insertQuery->bind_param('ii', $rx_bits_per_second, $tx_bits_per_second);
$monitor = new Request('/interface monitor-traffic interval=1s interface=LAN2 .proplist=rx-bits-per-second,tx-bits-per-second');
$monitor->setTag('m');
$client->sendAsync($monitor, function($response) use ($insertQuery, $rx_bits_per_second, $tx_bits_per_second) {
$rx_bits_per_second = $response->getArgument('rx-bits-per-second');
$tx_bits_per_second = $response->getArgument('tx-bits-per-second');
$insertQuery->execute();
});
//Start monitoring... indefinetly
$client->loop();
<?php
//Prepare the required chart library
require_once 'pChart/class/pDraw.class.php';
require_once 'pChart/class/pImage.class.php';
require_once 'pChart/class/pData.class.php';
//Connect to the DB to get the data
try {
$mysqli = new mysqli('localhost', 'root', '', 'db');
} catch (\Exception $e) {
die('Connection error: ' . $e);
}
//Get the data from the DB
$time = $rx_bits_per_second = $tx_bits_per_second = array();
$result = $mysqli->query('SELECT `time`, `rx-bits-per-second`, `tx-bits-per-second` FROM `stats`');
while ($row = $result->fetch_assoc()) {
$time[] = $row['time'];
$rx_bits_per_second[] = $row['rx-bits-per-second'];
$tx_bits_per_second[] = $row['tx-bits-per-second'];
}
//Assign the gathered data as the one to be charted
$stats = new pData();
$stats->addPoints($time, 'Time');
$stats->addPoints($rx_bits_per_second, 'rx-bits-per-second');
$stats->addPoints($tx_bits_per_second, 'tx-bits-per-second');
//Set display settings for the time
$stats->setAbscissa('Time');
$stats->setXAxisName('Time');
$stats->setXAxisDisplay(AXIS_FORMAT_TIME, 'H:i:s');
//Set display settings for the RX bps
$stats->setSerieOnAxis('rx-bits-per-second', 0);
$stats->setAxisName(0, 'RX');
$stats->setAxisUnit(0, 'bps');
//Set display settings for the TX bps
$stats->setSerieOnAxis('tx-bits-per-second', 1);
$stats->setAxisName(1, 'TX');
$stats->setAxisUnit(1, 'bps');
//Put the stats into a chart, and set the different dimensions and scales
$chart = new pImage(700, 230, $stats);
$chart->setGraphArea(60, 40, 670, 190);
$chart->drawScale();
//Make it an area chart
$chart->drawAreaChart();
//Done. Send it to the browser.
$chart->Stroke();
And do not inserting in SQL.Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files\VertrigoServ\www\billing\code\PEAR2\Net\Transmitter\Stream.php on line 300
Can I integration it on MySQL?I think I do not have MySQLi on my windows server. I'm install it and try again.
Thank you
"C:\Program Files\VertrigoServ\PHP\php.exe" -f "C:\Program Files\VertrigoServ\www\billing\code\monitor.php"
Yes. Just set the cron to run once, rather than on intervals.Thank you, I'll try it.
Example when I need this in linux, can I use cron?