* 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"; }
} } } if ($input == '' || $input == '/quit' || substr($input, -1) == ';') { // if input was empty, or quit - send sentence if ($input == '/quit') { $sentence = array($input); } if (substr($input, -1) == ';') { if (trim($input) != '') { $sentence[] = substr($input, 0, -1); } } if (sizeof($sentence) > 0) { // only send if we have something to send $mt->send($sentence); try { // try to read until !done $response = $mt->read(); // throws exception print_response($response); // print out the parsed response $sentence = array(); // reset input sentence } catch (Exception $ex) { if ($input == '/quit') { // if exception on quit, then command was successful echo "Disconnected.\n"; exit(0); } else { // caught trap or fatal error