/** * @param string $parameters * @return string */ private function execute($parameters) { return $this->ssh->execute($this->executable . ' ' . $parameters); }
$ex = json_decode($ex->getMessage()); $returned_results[] = array("server" => $server->id, "server_label" => $server->label, "stream" => "error", "result" => $ex->error->message); continue; } //// // Build the correct interpreter and command //// switch ($recipe->interpreter) { case "shell": $command = $recipe->content; break; case "bash": $command = "echo \$'" . str_replace("'", "\\'", $recipe->content) . "' | bash"; break; case "perl": $command = "echo \$'" . str_replace("'", "\\'", $recipe->content) . "' | perl"; break; case "python": $command = "echo \$'" . str_replace("'", "\\'", $recipe->content) . "' | python"; break; case "node.js": $command = "echo \$'" . str_replace("'", "\\'", $recipe->content) . "' | node"; break; } $result = $ssh->execute($command); $returned_results[] = array("server" => $server->id, "server_label" => $server->label, "stream" => $result->stream, "result" => $result->result); } MongoConnection::connect(); MongoConnection::selectCollection("executions"); MongoConnection::insert(Functions::build_execution_history_object($_POST['notes'], $_POST['groups'], $recipe, $servers, $returned_results)); echo json_encode($returned_results);
/** * Run ssh command. * * @param string $function * @param array $args * @return mixed */ public function execute($function, $args) { if (!isset($this->connection)) { $this->makeConnection(); } # Auto reconnect if (!preg_match('/^\\w[\\w-\\.]*$/', $function)) { throw new SecurityException("Illegal function name '{$function}': A function name should only contain alphanumeric chars and dashes."); } $command = $this->command; $command = strpos($command, '{$0}') !== false ? str_replace('{$0}', $function, $command) : $command . ' ' . $function; $eargs = array(); $files = array(); foreach ($args as $arg) { $eargs[] = $this->escapeArg($arg, $files); } if (preg_match('/\\{$\\d+\\}/', $command)) { $command = preg_replace('/\\{$(\\d+)\\}/e', 'isset($eargs[$1]) ? $eargs[$1] : null', $command); } elseif (preg_match('/\\{$\\?\\}/', $command)) { $command = preg_replace('/\\{$\\?\\}/', join(' ', $eargs), $command); } else { $command .= ' ' . join(' ', $eargs); } if ($this->options->checkreturn) { $command .= '; ERR=$?; [ $ERR -eq 0 ] || echo "Q Error: Exited with return code $ERR" 1>&2'; } foreach ($files as $file => $tmpfile) { $this->sendFile($file, $tmpfile); } list($stdio, $stderr) = parent::execute($command); stream_set_blocking($stdio, true); stream_set_blocking($stderr, true); $output = stream_get_contents($stdio); $errors = stream_get_contents($stderr); if (!empty($this->options->unserialize)) { $fn = $this->options->unserialize; $output = $fn($output); } $this->extrainfo[] = $errors; if (!empty($errors) && ($this->options->faultonstderr || $this->options->checkreturn && preg_match('/Q Error: Exited with return code \\d*$/s', $errors))) { throw new RPC_Fault($this->about($command), $output, 0, $errors); } return $output; }