/** * Sends a request to RouterOS. * * @param Request $request The request to send. * * @return self|Client The client object. * @see sendSync() * @see sendAsync() */ protected function send(Request $request) { $request->send($this->com, $this->registry); $this->pendingRequestsCount++; return $this; }
/** * Executes a RouterOS script. * * Same as the public equivalent, with the addition of allowing you to get * the contents of the script post execution, instead of removing it. * * @param string $source A script to execute. * @param array $params An array of local variables to make available in * the script. Variable names are array keys, and variable values are * array values. Note that the script's (generated) name is always added * as the variable "_", which you can overwrite from here. * Native PHP types will be converted to their RouterOS equivalents. * DateTime and DateInterval objects will be casted to RouterOS' "time" * type. Other types are casted to strings. * @param string $policy Allows you to specify a policy the script must * follow. Has the same format as in terminal. If left NULL, the script * has no restrictions. * @param string $name The script is executed after being saved in * "/system script" under a random name (prefixed with the computer's * name), and is removed after execution. To eliminate any possibility * of name clashes, you can specify your own name. * @param bool $get Whether to keep the script after execution. * * @return ResponseCollection|string If the script was not added * successfully before execution, the ResponseCollection from the add * attempt is going to be returned. Otherwise, the (generated) name of * the script. */ private function _exec($source, array $params = array(), $policy = null, $name = null, $get = false) { $request = new Request('/system/script/add'); if (null === $name) { $name = uniqid(gethostname(), true); } $request->setArgument('name', $name); $request->setArgument('policy', $policy); $finalSource = '/' . str_replace('/', ' ', substr($this->menu, 1)) . "\n"; $params += array('_' => $name); foreach ($params as $pname => $pvalue) { $pname = static::escapeString($pname); $pvalue = static::escapeValue($pvalue); $finalSource .= ":local \"{$pname}\" {$pvalue};\n"; } $finalSource .= $source . "\n"; $request->setArgument('source', $finalSource); $result = $this->client->sendSync($request); if (0 === count($result->getAllOfType(Response::TYPE_ERROR))) { $request = new Request('/system/script/run'); $request->setArgument('number', $name); $result = $this->client->sendSync($request); if ($get) { $result = $this->client->sendSync(new Request('/system/script/print .proplist="source"', Query::where('name', $name)))->getArgument('source'); } $request = new Request('/system/script/remove'); $request->setArgument('numbers', $name); $this->client->sendSync($request); } return $result; }