RouterOS PHP API class v1.6 Author: Denis Basta Contributors: Nick Barnes Ben Menking (ben [at] infotechsc [dot] com) Jeremy Jefferson (http://jeremyj.com) Cristian Deluxe (djcristiandeluxe [at] gmail [dot] com) Mikhail Moskalev (mmv.rus [at] gmail [dot] com) http://www.mikrotik.com http://wiki.mikrotik.com/wiki/API_PHP_class ****************************
Ejemplo n.º 1
2
<?php

/* Example for adding a VPN user */
require '../routeros_api.class.php';
$API = new RouterosAPI();
$API->debug = true;
if ($API->connect('111.111.111.111', 'LOGIN', 'PASSWORD')) {
    $API->comm("/ppp/secret/add", array("name" => "user", "password" => "pass", "remote-address" => "172.16.1.10", "comment" => "{new VPN user}", "service" => "pptp"));
    $API->disconnect();
}
Ejemplo n.º 2
2
function collect_pppoe_users_api(&$host)
{
    $rows = array();
    $api = new RouterosAPI();
    $api->debug = false;
    $rekey_array = array('host_id', 'name', 'index', 'userType', 'serverID', 'domain', 'bytesIn', 'bytesOut', 'packetsIn', 'packetsOut', 'curBytesIn', 'curBytesOut', 'curPacketsIn', 'curPacketsOut', 'prevBytesIn', 'prevBytesOut', 'prevPacketsIn', 'prevPacketsOut', 'present', 'last_seen');
    // Put the queues into an array
    $users = array_rekey(db_fetch_assoc_prepared("SELECT \n\t\thost_id, '0' AS `index`, '1' AS userType, '0' AS serverID, SUBSTRING(name, 7) AS name, '' AS domain,\n\t\tBytesIn AS bytesIn, BytesOut AS bytesOut, PacketsIn as packetsIn, PacketsOut AS packetsOut,\n\t\tcurBytesIn, curBytesOut, curPacketsIn, curPacketsOut, \n\t\tprevBytesIn, prevBytesOut, prevPacketsIn, prevPacketsOut, present, last_seen\n\t\tFROM plugin_mikrotik_queues \n\t\tWHERE host_id = ? \n\t\tAND name LIKE 'PPPOE-%'", array($host['id'])), 'name', $rekey_array);
    $creds = db_fetch_row_prepared('SELECT * FROM plugin_mikrotik_credentials WHERE host_id = ?', array($host['id']));
    $start = microtime(true);
    if (sizeof($creds)) {
        if ($api->connect($host['hostname'], $creds['user'], $creds['password'])) {
            $api->write('/ppp/active/getall');
            $read = $api->read(false);
            $array = $api->parseResponse($read);
            $end = microtime(true);
            $sql = array();
            cacti_log('MIKROTIK RouterOS API STATS: API Returned ' . sizeof($array) . ' PPPoe Users in ' . round($end - $start, 2) . ' seconds.', false, 'SYSTEM');
            if (sizeof($array)) {
                foreach ($array as $row) {
                    if (!isset($row['name'])) {
                        continue;
                    }
                    $name = strtoupper($row['name']);
                    if (isset($users[$name])) {
                        $user = $users[$name];
                        $user['mac'] = $row['caller-id'];
                        $user['ip'] = $row['address'];
                        $user['connectTime'] = uptimeToSeconds($row['uptime']);
                        $user['host_id'] = $host['id'];
                        $user['radius'] = $row['radius'] == 'true' ? 1 : 0;
                        $user['limitBytesIn'] = $row['limit-bytes-in'];
                        $user['limitBytesOut'] = $row['limit-bytes-out'];
                        $user['userType'] = 1;
                        $sql[] = '(' . $user['host_id'] . ',' . $user['index'] . ',' . $user['userType'] . ',' . $user['serverID'] . ',' . db_qstr($user['name']) . ',' . db_qstr($user['domain']) . ',' . db_qstr($user['mac']) . ',' . db_qstr($user['ip']) . ',' . $user['connectTime'] . ',' . $user['bytesIn'] . ',' . $user['bytesOut'] . ',' . $user['packetsIn'] . ',' . $user['packetsOut'] . ',' . $user['curBytesIn'] . ',' . $user['curBytesOut'] . ',' . $user['curPacketsIn'] . ',' . $user['curPacketsOut'] . ',' . $user['prevBytesIn'] . ',' . $user['prevBytesOut'] . ',' . $user['prevPacketsIn'] . ',' . $user['prevPacketsOut'] . ',' . $user['limitBytesIn'] . ',' . $user['limitBytesOut'] . ',' . $user['radius'] . ',' . $user['present'] . ',' . db_qstr($user['last_seen']) . ')';
                    }
                }
                if (sizeof($sql)) {
                    db_execute('INSERT INTO plugin_mikrotik_users 
						(host_id, `index`, userType, serverID, name, domain, mac, ip, connectTime, 
						bytesIn, bytesOut, packetsIn, packetsOut, 
						curBytesIn, curBytesOut, curPacketsIn, curPacketsOut, 
						prevBytesIn, prevBytesOut, prevPacketsIn, prevPacketsOut, 
						limitBytesIn, limitBytesOut, radius, present, last_seen) 
						VALUES ' . implode(', ', $sql) . '
						ON DUPLICATE KEY UPDATE connectTime=VALUES(connectTime), 
						bytesIn=VALUES(bytesIn), bytesOut=VALUES(bytesOut), 
						packetsIn=VALUES(packetsIn), packetsOut=VALUES(packetsOut), 
						curBytesIn=VALUES(curBytesIn), curBytesOut=VALUES(curBytesOut),
						curPacketsIn=VALUES(curPacketsIn), curPacketsOut=VALUES(curPacketsOut),
						prevBytesIn=VALUES(prevBytesIn), prevBytesOut=VALUES(prevBytesOut),
						prevPacketsIn=VALUES(prevPacketsIn), prevPacketsOut=VALUES(prevPacketsOut),
						limitBytesIn=VALUES(limitBytesIn), limitBytesOut=VALUES(limitBytesOut),
						radius=VALUES(radius), present=VALUES(present), last_seen=VALUES(last_seen)');
                }
            }
            $idle_users = db_fetch_assoc_prepared('SELECT name 
				FROM plugin_mikrotik_users 
				WHERE last_seen < FROM_UNIXTIME(UNIX_TIMESTAMP() - ?) 
				AND present=1 
				AND host_id = ?', array(read_config_option('mikrotik_queues_freq'), $host['id']));
            db_execute_prepared('UPDATE IGNORE plugin_mikrotik_users SET
				bytesIn=0, bytesOut=0, packetsIn=0, packetsOut=0, connectTime=0,
				curBytesIn=0, curBytesOut=0, curPacketsIn=0, curPacketsOut=0,
				prevBytesIn=0, prevBytesOut=0, prevPacketsIn=0, prevPacketsOut=0, present=0
				WHERE host_id = ? 
				AND userType = 1 
				AND last_seen < FROM_UNIXTIME(UNIX_TIMESTAMP() - ?)', array($host['id'], read_config_option('mikrotik_queues_freq')));
            $api->disconnect();
        } else {
            cacti_log('ERROR:RouterOS @ ' . $host['description'] . ' Timed Out');
        }
    }
}
Ejemplo n.º 3
1
<?php

/* Example of finding registration-table ID for specified MAC */
require '../routeros_api.class.php';
$API = new RouterosAPI();
$API->debug = true;
if ($API->connect('111.111.111.111', 'LOGIN', 'PASSWORD')) {
    $ARRAY = $API->comm("/interface/wireless/registration-table/print", array(".proplist" => ".id", "?mac-address" => "00:0E:BB:DD:FF:FF"));
    print_r($ARRAY);
    $API->disconnect();
}
Ejemplo n.º 4
1
<?php

/* Example of counting leases from a specific IP Pool (using regexp) */
require '../routeros_api.class.php';
$API = new RouterosAPI();
$API->debug = true;
if ($API->connect('111.111.111.111', 'LOGIN', 'PASSWORD')) {
    $ARRAY = $API->comm("/ip/dhcp-server/lease/print", array("count-only" => "", "~active-address" => "1.1."));
    print_r($ARRAY);
    $API->disconnect();
}
Ejemplo n.º 5
0
<?php

/* 3 step action 
   1) fetch all static dns hosts
   2) remove all static dns hosts
   3) add example host
*/
require '../routeros_api.class.php';
$API = new RouterosAPI();
if ($API->connect('111.111.111.111', 'LOGIN', 'PASSWORD')) {
    # Get all current hosts
    $API->write('/ip/dns/static/print');
    $ips = $API->read();
    # delete them all !
    foreach ($ips as $num => $ip_data) {
        $API->write('/ip/dns/static/remove', false);
        $API->write("=.id=" . $ip_data[".id"], true);
    }
    #add some new
    $API->comm("/ip/dns/static/add", array("name" => "jefkeklak", "address" => "1.2.3.4", "ttl" => "1m"));
    #show me what you got
    $API->write('/ip/dns/static/print');
    $ips = $API->read();
    var_dump($ips);
    $API->disconnect();
}
Ejemplo n.º 6
0
<?php

require '../routeros_api.class.php';
$API = new RouterosAPI();
$API->debug = true;
if ($API->connect('111.111.111.111', 'LOGIN', 'PASSWORD')) {
    $API->write('/interface/getall');
    $READ = $API->read();
    $ARRAY = $API->parse_response($READ);
    print_r($ARRAY);
    $API->disconnect();
}
Ejemplo n.º 7
0
<?php

require '../routeros_api.class.php';
$API = new RouterosAPI();
$API->debug = true;
if ($API->connect('192.168.88.1', 'admin', 'admin')) {
    $API->write('/interface/getall');
    $READ = $API->read();
    $ARRAY = $API->parse_response($READ);
    print_r($ARRAY);
    $API->disconnect();
}
Ejemplo n.º 8
0
<?php

require '../routeros_api.class.php';
$API = new RouterosAPI();
$API->debug = true;
if ($API->connect('111.111.111.111', 'LOGIN', 'PASSWORD')) {
    $API->write('/interface/wireless/registration-table/print', false);
    $API->write('=stats=');
    $READ = $API->read();
    $ARRAY = $API->parse_response($READ);
    print_r($ARRAY);
    $API->disconnect();
}
Ejemplo n.º 9
0
function mikrotik_host_top()
{
    global $fields_host_edit;
    $id = get_filter_request_var('id');
    $template_id = db_fetch_cell('SELECT id FROM host_template WHERE hash="d364e2b9570f166ab33c8df8bd503887"');
    $is_tik = db_fetch_row_prepared('SELECT pmc.*, host.hostname
		FROM host 
		LEFT JOIN plugin_mikrotik_credentials AS pmc 
		ON host.id=pmc.host_id 
		WHERE host_template_id = ? AND host.id = ?', array($template_id, $id));
    if (sizeof($is_tik)) {
        $fields_host_edit += array('mikrotik_head' => array('method' => 'spacer', 'collapsible' => 'true', 'friendly_name' => __('MikroTik Credentials')), 'mikrotik_user' => array('method' => 'textbox', 'friendly_name' => __('Read Only User'), 'description' => __('Provide a read only username for the MikroTik.'), 'value' => $is_tik['user'], 'max_length' => '40', 'size' => '20'), 'mikrotik_password' => array('method' => 'textbox', 'friendly_name' => __('Password'), 'description' => __('Provide the read only username password for this MikroTik.'), 'value' => $is_tik['password'], 'max_length' => '40', 'size' => '30'));
        if ($is_tik['user'] != '') {
            include_once './plugins/mikrotik/RouterOS/routeros_api.class.php';
            $api = new RouterosAPI();
            $api->debug = false;
            if ($api->connect($is_tik['hostname'], $is_tik['user'], $is_tik['password'])) {
                $api->disconnect();
                $fields_host_edit += array('mikrotik_result' => array('method' => 'other', 'friendly_name' => __('Connection Result'), 'description' => __('Ok if Cacti can connect to the Mikrotik over its API port.'), 'value' => 'Connected Successfully'));
            } else {
                $fields_host_edit += array('mikrotik_result' => array('method' => 'other', 'friendly_name' => __('Connection Result'), 'description' => __('Ok if Cacti can connect to the Mikrotik over its API port.'), 'value' => 'Connection Failed'));
            }
        }
    }
}
Ejemplo n.º 10
-1
 public function getRecursos()
 {
     $API = new RouterosAPI();
     $conexion = self::conectar();
     if ($conexion) {
         // Change this as necessery
         $ARRAY = $API->comm("/system/resource/print");
         $first = $ARRAY['0'];
         $memperc = $first['free-memory'] / $first['total-memory'];
         $hddperc = $first['free-hdd-space'] / $first['total-hdd-space'];
         $mem = $memperc * 100;
         $hdd = $hddperc * 100;
         // $box = '<div class="row">'.PHP_EOL;
         // $box .= '<div class="col-md-3 col-sm-6 col-xs-12">'.PHP_EOL;
         // $box .= '<div class="info-box bg-aqua">'.PHP_EOL;
         // $box .= '<span class="info-box-icon"><i class="fa fa-hdd-o"></i></span>'.PHP_EOL;
         // $box .= '<div class="info-box-content">'.PHP_EOL;
         // $box .= '<span class="info-box-text">Espacio en Disco</span>'.PHP_EOL;
         // $box .= '<span class="info-box-number">'.$first['total-hdd-space'] .'</span>'
         // return $box;
         echo "<table width=550 border=0 align=center>";
         echo "<tr><td>Platform, board name and Ros version is:</td><td>" . $first['platform'] . " - " . $first['board-name'] . " - " . $first['version'] . " - " . $first['architecture-name'] . "</td></tr><br />";
         echo "<tr><td>Cpu and available cores:</td><td>" . $first['cpu'] . " at " . $first['cpu-frequency'] . " Mhz with " . $first['cpu-count'] . " core(s) " . "</td></tr><br />";
         echo "<tr><td>Uptime is:</td><td>" . $first['uptime'] . " (hh/mm/ss)" . "</td></tr><br />";
         echo "<tr><td>Cpu Load is:</td><td>" . $first['cpu-load'] . " %" . "</td></tr><br />";
         echo "<tr><td>Total,free memory and memory % is:</td><td>" . $first['total-memory'] . "Kb - " . $first['free-memory'] . "Kb - " . number_format($mem, 3) . "% </td></tr><br />";
         echo "<tr><td>Total,free disk and disk % is:</td><td>" . $first['total-hdd-space'] . "Kb - " . $first['free-hdd-space'] . "Kb - " . number_format($hdd, 3) . "% </td></tr><br />";
         echo "<tr><td>Sectors (write,since reboot,bad blocks):</td><td>" . $first['write-sect-total'] . " - " . $first['write-sect-since-reboot'] . " - " . $first['bad-blocks'] . "% </td></tr><br />";
         echo "</table>";
     }
 }