Source for file cmd-json.php
Documentation is available at cmd-json.php
* Provides a JSON interface for running arbitrary commands.
* <p>This code allows one to run an arbitrary command on the NST probe
* (passed as the "cmd" parameter) and get the results back to a AJAX
* <dt>op=lines&cmd=CMD</dt>
* <dd>Returns results of running the CMD as a JavaScript object.</dd>
* <dt>op=sudo-lines&cmd=CMD</dt>
* <dd>Returns results of running "sudo CMD" as a JavaScript object.</dd>
* <dt>op=exec-dir&dir=DIR&match=PATTERN&args=ARGS</dt>
* <dd>Runs each executable matching PATTERN in DIR passing each
include_once('../lib/Json.php');
/** Returns results of arbitrary command as a standard results object.
* <p>This method returns the associative array of the following form:</p>
* <dt>command</dt><dd>The original command which was run.</dd>
* <dt>rc</dt><dd>The integer return code from running the command.</dd>
* <dt>output</dt><dd>Array of lines (if command generated any output).</dd>
* @return A JSON ecoded string (an associative JavaScript
$cmd =
$json->getValue('cmd', '/bin/date');
$results =
$json->exec($cmd);
$json->postResults($results);
/** Decide what to do based on the operation specified. */
// Create a temporary working directory for all NST Shell
$tdir =
'/tmp/nst-console';
} else if ($op ==
'sudo-lines') {
// Build command with "sudo" in front and then run it
$results =
$json->exec($cmd);
$json->postResults($results);
} else if ($op ==
'exec-dir') {
// Get directory and pattern to look for scripts in
$pattern =
$json->getValue('match', '*.bash');
// Get command line arguments for each script
// Assume no scripts to run
$results =
array('rc' =>
0, 'cnt' =>
0, 'command' =>
'multi',
'dir' =>
$dir, 'match' =>
$pattern, 'args' =>
$args,
'output' =>
'See individual commands');
// Find all executable scripts in directory
while (($file =
readdir($dh)) !==
false) {
$file =
$dir .
DIRECTORY_SEPARATOR .
$file;
// Sort scripts to run by name (consistent ordering of runs)
foreach ($scripts as $script) {
// Build full command line to safely run script in background
.
$script .
' ' .
$args .
' </dev/null &>/dev/null &';
$results['commands'][] =
$r;
// If any command fails to run, set global rc to failure code
$results['rc'] ==
$r->rc;
// Bump count of commands run
// Give back number of scripts run and details on each
$json->postResults($results);
$output =
(isset
($_POST) ?
$_POST :
$_REQUEST);
$cmd =
"Request for unsupported operation (op={$op}) ({$cnt} params)";
$json->postResults($json->execResults($cmd, 1, $output));
Documentation generated on Mon, 10 Sep 2012 09:57:12 -0400 by phpDocumentor 1.4.4