100% found this document useful (1 vote)
3K views4 pages

API PHP Class - MikroTik Wiki

This document provides a PHP class for interacting with the RouterOS API. It includes the class code, examples of using the class to connect to a router and execute commands, and an output of the response from getting all interfaces. The class handles connecting, writing commands, reading responses, and parsing responses into arrays. It aims to simplify working with the RouterOS API in PHP applications and scripts.

Uploaded by

Wahyudi Bukhari
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
3K views4 pages

API PHP Class - MikroTik Wiki

This document provides a PHP class for interacting with the RouterOS API. It includes the class code, examples of using the class to connect to a router and execute commands, and an output of the response from getting all interfaces. The class handles connecting, writing commands, reading responses, and parsing responses into arrays. It aims to simplify working with the RouterOS API in PHP applications and scripts.

Uploaded by

Wahyudi Bukhari
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

API PHP class - MikroTik Wiki http://wiki.mikrotik.

com/wiki/API_PHP_class

API PHP class


This is PHP class for working with RouterOS v3 API. You can take it, edit it and use it as you need.

NOTE - The class as shown does not work for a) large replies, and b) setting values where a return value is sent after a "!done". I have coded fixes for these here
(http://forum.mikrotik.com/viewtopic.php?f=9&t=29745) and will update this page when other people have had a chance to review.

Contents
1 Class
2 Example 1
2.1 Output
3 Example 2
3.1 Output
4 See also

Class

1 of 4 9/8/2010 4:56 PM
API PHP class - MikroTik Wiki http://wiki.mikrotik.com/wiki/API_PHP_class

<?php
//
// RouterOS API class
// Author: Denis Basta
//
// read() function altered by Nick Barnes to take into account the placing
// of the "!done" reply and also correct calculation of the reply length.
///
// read() function altered by Ben Menking ([email protected]); removed
// echo statement that dumped byte data to screen
//
///////////////////////////
// Revised by: Jeremy Jefferson (http://jeremyj.com)
// January 8, 2010
//
// Fixed write function in order to allow for queries to be executed
//

class routeros_api {

var $debug = false; // Show debug information


var $error_no; // Variable for storing connection error number, if any
var $error_str; // Variable for storing connection error text, if any

var $attempts = 5; // Connection attempt count


var $connected = false; // Connection state
var $delay = 3; // Delay between connection attempts in seconds
var $port = 8728; // Port to connect to
var $timeout = 3; // Connection attempt timeout and data read timeout

var $socket; // Variable for storing socket resource

/**************************************************
*
*************************************************/

function debug($text) {

if ($this->debug)
echo $text . "\n";

/**************************************************
*
*************************************************/

function encode_length($length) {

if ($length < 0x80) {

$length = chr($length);

}
else
if ($length < 0x4000) {

$length |= 0x8000;

$length = chr( ($length >> 8) & 0xFF) . chr($length & 0xFF);

}
else
if ($length < 0x200000) {

$length |= 0xC00000;

$length = chr( ($length >> 8) & 0xFF) . chr( ($length >> 8) & 0xFF) . chr($length & 0xFF);

}
else
if ($length < 0x10000000) {

$length |= 0xE0000000;

$length = chr( ($length >> 8) & 0xFF) . chr( ($length >> 8) & 0xFF) . chr( ($length >> 8) & 0xFF) . chr($length & 0xFF);

}
else
if ($length >= 0x10000000)
$length = chr(0xF0) . chr( ($length >> 8) & 0xFF) . chr( ($length >> 8) & 0xFF) . chr( ($length >> 8) & 0xFF) . chr($length &

return $length;

/**************************************************
*
*************************************************/

function connect($ip, $login, $password) {

for ($ATTEMPT = 1; $ATTEMPT <= $this->attempts; $ATTEMPT++) {

$this->connected = false;

$this->debug('Connection attempt #' . $ATTEMPT . ' to ' . $ip . ':' . $this->port . '...');

if ($this->socket = @fsockopen($ip, $this->port, $this->error_no, $this->error_str, $this->timeout) ) {

socket_set_timeout($this->socket, $this->timeout);

$this->write('/login');

$RESPONSE = $this->read(false);

if ($RESPONSE[0] == '!done') {

if (preg_match_all('/[^=]+/i', $RESPONSE[1], $MATCHES) ) {

if ($MATCHES[0][0] == 'ret' && strlen($MATCHES[0][1]) == 32) {

$this->write('/login', false);
$this->write('=name=' . $login, false);
$this->write('=response=00' . md5(chr(0) . $password . pack('H*', $MATCHES[0][1]) )

2 of 4 9/8/2010 4:56 PM
API PHP class - MikroTik Wiki http://wiki.mikrotik.com/wiki/API_PHP_class

Example 1

<?php

require('routeros_api.class.php');

$API = new routeros_api();

$API->debug = true;

if ($API->connect('111.111.111.111', 'LOGIN', 'PASSWORD')) {

$API->write('/interface/getall');

$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);

print_r($ARRAY);

$API->disconnect();

?>

OR

<?php

require('routeros_api.class.php');

$API = new routeros_api();

$API->debug = true;

if ($API->connect('111.111.111.111', 'LOGIN', 'PASSWORD')) {

$API->write('/interface/getall');
$ARRAY = $API->read();

print_r($ARRAY);

$API->disconnect();

?>

Output

Array
(
[0] => Array
(
[.id] => *1
[name] => ether1
[mtu] => 1500
[type] => ether
[running] => yes
[dynamic] => no
[slave] => no
[comment] =>
[disabled] => no
)

[1] => Array


(
[.id] => *2
[name] => ether2
[mtu] => 1500
[type] => ether
[running] => yes
[dynamic] => no
[slave] => no
[comment] =>
[disabled] => no
)

[2] => Array


(
[.id] => *3
[name] => ether3
[mtu] => 1500
[type] => ether
[running] => yes
[dynamic] => no
[slave] => no
[comment] => ether3
[disabled] => no
)
)

Example 2
Thanks a lot for this API, It help me a lot to write my php page for our support team to have access to wireless registration table.

3 of 4 9/8/2010 4:56 PM
API PHP class - MikroTik Wiki http://wiki.mikrotik.com/wiki/API_PHP_class

$API->write('/interface/wireless/registration-table/print',false);
$API->write('=stats=');

Output

Array
(
[0] =>
Array
(
[.id] => *147
[comment] =>
[interface] => AP101
[mac-address] => 00:0B:6B:37:58:33
[ap] => false
[wds] => false
[rx-rate] => 11Mbps
[tx-rate] => 11Mbps
[packets] => 4043966,2669114
[bytes] => 3961713942,280551024
[frames] => 4043966,2669114
[frame-bytes] => 3937477458,264536340
[hw-frames] => 4500839,2669114
[hw-frame-bytes] => 256326637,349947988
[tx-frames-timed-out] => 0
[uptime] => 1w13:09:12
[last-activity] => 00:00:00.090
[signal-strength] => -73dBm@11Mbps
[signal-to-noise] => 30
[strength-at-rates] => -73dBm@1Mbps 4m4s640ms,-73dBm@2Mbps 4m58s730ms,[email protected] 42s450ms,-73dBm@11Mbps 90ms
[tx-ccq] => 91
[p-throughput] => 5861
[ack-timeout] => 31
[last-ip] => 192.168.0.220
[802.1x-port-enabled] => true
[wmm-enabled] => false
)
[1] => Array
(
...
)
...
)

See also
API
PHP Telnet Class

4 of 4 9/8/2010 4:56 PM

You might also like