Beispiel #1
0
/**
 * Example script demonstrating the basic functions of Lib_RouterOS.
 * This example connects to the router, logs in, and issues a command
 * to retrieve some select system information.
 *
 */
// define required values
$router = '192.168.88.1:8728';
$username = '******';
$password = '';
// basic informational command to send
$command = '/system/resource/print';
$args = array('.proplist' => 'version,cpu,cpu-frequency,cpu-load,uptime');
// begin script
require_once 'RouterOS.php';
$mikrotik = new Lib_RouterOS();
$mikrotik->setDebug(true);
try {
    // establish connection to router; throws exception if connection fails
    $mikrotik->connect($router);
    // send login sequence; throws exception on invalid username/password
    $mikrotik->login($username, $password);
    // encodes and send command to router; throws exception if connection lost
    $mikrotik->send($command, $args);
    // read response to command; throws exception if command was invalid (!trap,
    // !fatal etc), connection terminated, or recv'd unexpected data
    $response = $mikrotik->read();
    // show the structure of the parsed response
    print_r($response);
} catch (Exception $ex) {
    echo "Caught exception from router: " . $ex->getMessage() . "\n";
}
require_once 'RouterOS.php';
// check for special arguments
foreach ($_SERVER['argv'] as $arg) {
    $arg = trim($arg);
    if (in_array($arg, array('--help', '-h', '?', '/?'))) {
        usage();
        exit;
    }
}
// get any options passed from the command line
$router = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
$username = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : '';
$password = isset($_SERVER['argv'][3]) ? $_SERVER['argv'][3] : '';
// get new instance of mikrotik api object
$mt = new Lib_RouterOS();
// set default identity for api prompt
$router_identity = 'router';
// Prompt for router if not given
if ($router == '') {
    do {
        echo "Connect to: ";
        $router = trim(fgets(STDIN));
    } while ($router == '');
}
if (strpos($router, ':') !== false) {
    // a router:port combination was supplied, validate the port
    list($router, $port) = explode(':', $router, 2);
    if (!preg_match('/^\\d{1,5}$/', $port) || (int) $port > 65535) {
        die('Invalid port "' . $port . '" provided.' . "\n");
    }