Source for file cmd-json.php

Documentation is available at cmd-json.php

  1. <?php
  2. /**
  3.  * Provides a JSON interface for running arbitrary commands.
  4.  *
  5.  * <p>This code allows one to run an arbitrary command on the NST probe
  6.  * (passed as the "cmd" parameter) and get the results back to a AJAX
  7.  * handler.</p>
  8.  *
  9.  * <dl>
  10.  * 
  11.  * <dt>op=lines&cmd=CMD</dt>
  12.  *
  13.  * <dd>Returns results of running the CMD as a JavaScript object.</dd>
  14.  * 
  15.  * <dt>op=sudo-lines&cmd=CMD</dt>
  16.  *
  17.  * <dd>Returns results of running "sudo CMD" as a JavaScript object.</dd>
  18.  * 
  19.  * <dt>op=exec-dir&dir=DIR&match=PATTERN&args=ARGS</dt>
  20.  *
  21.  * <dd>Runs each executable matching PATTERN in DIR passing each
  22.  * invocation ARGS.</dd>
  23.  *
  24.  * </dl>
  25.  */
  26.  
  27. include_once('../lib/Json.php');
  28.  
  29. /** Returns results of arbitrary command as a standard results object.
  30.  *
  31.  * <p>This method returns the associative array of the following form:</p>
  32.  *
  33.  * <dl>
  34.  * <dt>command</dt><dd>The original command which was run.</dd>
  35.  * <dt>rc</dt><dd>The integer return code from running the command.</dd>
  36.  * <dt>output</dt><dd>Array of lines (if command generated any output).</dd>
  37.  * </dl>
  38.  *
  39.  * @return JSON ecoded string (an associative JavaScript
  40.  *  array). */
  41.  
  42. function cmdArrayOutput($json$tdir{
  43.   chdir($tdir);
  44.   $cmd $json->getValue('cmd''/bin/date');
  45.   $results $json->exec($cmd);
  46.   $json->postResults($results);
  47. }
  48.  
  49. /** Decide what to do based on the operation specified. */
  50.  
  51. $json new Json();
  52. $op $json->getValue('op'null);
  53.  
  54. // Create a temporary working directory for all NST Shell
  55. // command execution...
  56. $tdir '/tmp/nst-console';
  57. if (is_dir($tdir)) {
  58.   mkdir($tdir);
  59. }
  60.  
  61. if ($op == 'lines'{
  62.   cmdArrayOutput($json$tdir);
  63. else if ($op == 'sudo-lines'{
  64.   // Build command with "sudo" in front and then run it
  65.   $cmd $json->getCommand('sudo'' '$json->getValue('cmd''/bin/date');
  66.   chdir($tdir);
  67.   $results $json->exec($cmd);
  68.   $json->postResults($results);
  69. else if ($op == 'exec-dir'{
  70.   // Get directory and pattern to look for scripts in
  71.   $dir $json->getValue('dir'false);
  72.   $pattern $json->getValue('match''*.bash');
  73.  
  74.   // Get command line arguments for each script
  75.   $args $json->getValue('args''');
  76.  
  77.   // Assume no scripts to run
  78.   $scripts array();
  79.   $rarray array();
  80.  
  81.   $results array('rc' => 0'cnt' => 0'command' => 'multi',
  82.            'dir' => $dir'match' => $pattern'args' => $args,
  83.            'commands' => array(),
  84.            'output' => 'See individual commands');
  85.  
  86.   // Find all executable scripts in directory
  87.   if (is_dir($dir)) {
  88.     if ($dh opendir($dir)) {
  89.       while (($file readdir($dh)) !== false{
  90.     $file $dir DIRECTORY_SEPARATOR $file;
  91.     if (is_executable($file&& fnmatch($pattern$file)) {
  92.       $scripts[$file;
  93.     }
  94.       }
  95.       closedir($dh);
  96.  
  97.       // Sort scripts to run by name (consistent ordering of runs)
  98.       sort($scripts);
  99.     }
  100.   }
  101.  
  102.   // Run each script
  103.   foreach ($scripts as $script{
  104.     // Build full command line to safely run script in background
  105.     $cmd $json->getCommand('sudo'' ' 
  106.       . $json->getCommand('setsid'' '
  107.       . $script ' ' $args ' </dev/null &>/dev/null &';
  108.  
  109.     chdir($tdir);
  110.     $r $json->exec($cmd);
  111.     $results['commands'][$r;
  112.  
  113.     // If any command fails to run, set global rc to failure code
  114.     if ($r->rc != 0{
  115.       $results['rc'== $r->rc;
  116.     }
  117.  
  118.     // Bump count of commands run
  119.     $results['cnt']++;
  120.   }
  121.  
  122.   // Give back number of scripts run and details on each
  123.   $json->postResults($results);
  124.  
  125. else {
  126.   $output (isset($_POST$_POST $_REQUEST);
  127.   $cnt sizeof($output);
  128.   $cmd "Request for unsupported operation (op={$op}) ({$cnt} params)";
  129.   $json->postResults($json->execResults($cmd1$output));
  130. }
  131.  
  132. ?>

Documentation generated on Mon, 10 Sep 2012 09:57:12 -0400 by phpDocumentor 1.4.4