/**
  * Actually executes the Shellcommand
  * 
  * @param integer $commandId     ID of the command to execute
  * @param string $targetParam    Target of the command
  * 
  * @return string    Execution output, bool(false) in case of error
  */
 static function execCommand($commandId, $targetParam)
 {
     $output = null;
     $execOuput = array();
     $error = 1;
     $command = new PluginShellcommandsShellcommand();
     $commandFound = $command->getFromDB($commandId);
     // Lauch command
     if ($commandFound) {
         if ($command->fields['name'] === self::WOL_COMMAND_NAME) {
             $command_item = new PluginShellcommandsShellcommand_Item();
             ob_start();
             $command_item->sendMagicPacket($targetParam);
             $output = ob_get_clean();
         } else {
             if (($commandToExec = self::getCommandLine($commandId, $targetParam)) !== null) {
                 exec($commandToExec . " 2>&1", $execOuput, $error);
             }
         }
     }
     // Format output message
     foreach ($execOuput as $currentOutputLine) {
         if (!is_array($currentOutputLine)) {
             $output .= Toolbox::encodeInUtf8($currentOutputLine) . PHP_EOL;
         }
     }
     return array($error, $output);
 }
 /**
  * Launch a command
  * 
  * @param array $values
  * 
  * @return void
  */
 static function lauchCommand($values)
 {
     global $CFG_GLPI;
     if (!empty($values['itemtype']) && !empty($values['itemID'])) {
         $item = getItemForItemtype($values['itemtype']);
         $item->getFromDB($values['itemID']);
         $shellcommands_item = new PluginShellcommandsShellcommand_Item();
         $shellcommands = new PluginShellcommandsShellcommand();
         $commandgroups = new PluginShellcommandsCommandGroup();
         $commandgroups_item = new PluginShellcommandsCommandGroup_Item();
         $commandgroups_items = $commandgroups_item->find("`plugin_shellcommands_commandgroups_id`=" . $values['id'], "`rank`");
         echo "<div class='center'>";
         echo "<table class='tab_cadre_fixe shellcommands_result_line'>";
         echo "<tr class='tab_bg_1'>";
         echo "<th colspan='4'>" . __('Result details') . "</th>";
         echo "</tr>";
         // First : test ping
         $error = 1;
         $commandgroups->getFromDB($values['id']);
         if (!empty($commandgroups->fields['check_commands_id'])) {
             $targetParam = PluginShellcommandsShellcommand_Item::resolveLinkOfCommand($commandgroups->fields['check_commands_id'], $item);
             // Exec command on each targets : stop on first success
             if ($targetParam !== false) {
                 foreach ($targetParam as $target) {
                     list($error, $execPing) = PluginShellcommandsShellcommand_Item::execCommand($commandgroups->fields['check_commands_id'], $target);
                     if (!$error) {
                         break;
                     }
                 }
             }
         }
         // If Ping ok execute group commands
         if (!$error) {
             echo "<tr class='tab_bg_1 shellcommands_result_line'>";
             echo "<td class='center' colspan='2'>" . __($item->getType()) . ' : ' . $item->getLink() . "</td>";
             echo "<td class='center'><div class='shellcommands_result_ok'>UP</div></td>";
             echo "<td>";
             echo __('Host UP', 'shellcommands');
             echo "</td>";
             echo "</tr>";
             if (!empty($commandgroups_items)) {
                 foreach ($commandgroups_items as $val) {
                     if (!$shellcommands_item->getFromDBbyShellCommandsAndItem($val['plugin_shellcommands_shellcommands_id'], $values['itemtype'])) {
                         continue;
                     }
                     $shellcommands->getFromDB($val['plugin_shellcommands_shellcommands_id']);
                     $targetParam = PluginShellcommandsShellcommand_Item::resolveLinkOfCommand($val['plugin_shellcommands_shellcommands_id'], $item);
                     // Exec command on each targets : stop on first success
                     if ($targetParam !== false) {
                         foreach ($targetParam as $target) {
                             list($error, $message) = PluginShellcommandsShellcommand_Item::execCommand($shellcommands->getID(), $target);
                             if (!$error) {
                                 break;
                             }
                         }
                     }
                     PluginShellcommandsShellcommand::displayCommandResult($shellcommands, $target, $message, $error);
                 }
             }
         } else {
             echo "<tr class='tab_bg_1 shellcommands_result_line'>";
             echo "<td class='center' colspan='2'>" . __($item->getType()) . ' : ' . $item->getLink() . "</td>";
             echo "<td class='center'><div class='shellcommands_result_ko'>DOWN</div></td>";
             echo "<td>";
             echo __('Host DOWN', 'shellcommands');
             echo "</td>";
             echo "</tr>";
         }
         echo "</table>";
         echo "</div>";
     }
 }
Пример #3
0
function plugin_shellcommands_MassiveActionsProcess($data)
{
    $command = new PluginShellcommandsShellcommand();
    $res = array('ok' => 0, 'ko' => 0, 'noright' => 0);
    switch ($data['action']) {
        case "plugin_shellcommands_generate":
            foreach ($data["item"] as $key => $val) {
                if ($val == 1 && isset($data["command"]) && $data["command"] > 0) {
                    // Items exists ?
                    $item = new $data["itemtype"]();
                    if ($item->getFromDB($key)) {
                        // Entity security
                        $tab_id[] = $key;
                        $command->getFromDB($data["command"]);
                        $link = $command->fields["link"];
                        $_SESSION["plugin_shellcommands"]["itemtype"] = $data["itemtype"];
                        $_SESSION["plugin_shellcommands"]["tab_id"] = serialize($tab_id);
                        echo "<script type='text/javascript'>";
                        echo "location.href='../plugins/shellcommands/front/massiveexec.php?plugin_shellcommands_shellcommands_id=" . $command->fields["name"] . "'";
                        echo "</script>";
                        $res['ok']++;
                    } else {
                        $res['ko']++;
                    }
                }
            }
            break;
    }
    return $res;
}
 /**
  * Custom fonction to process shellcommand massive action
  **/
 function doMassiveAction(MassiveAction $ma, array $ids)
 {
     if (!empty($ids)) {
         $input = $ma->getInput();
         $itemtype = $ma->getItemType(false);
         $commands_id = $input['command'];
         switch ($ma->getAction()) {
             case 'generate':
                 $shellcommands_item = new PluginShellcommandsShellcommand_Item();
                 $shellcommands = new PluginShellcommandsShellcommand();
                 $item = getItemForItemtype($itemtype);
                 echo "<div class='center'>";
                 echo "<table class='tab_cadre_fixe center'>";
                 echo "<tr class='tab_bg_1'>";
                 echo "<th colspan='4'>" . PluginShellcommandsShellcommand::getTypeName(2) . "</th>";
                 echo "</tr>";
                 foreach ($ids as $key => $items_id) {
                     if (!$shellcommands_item->getFromDBbyShellCommandsAndItem($commands_id, $itemtype)) {
                         continue;
                     }
                     $shellcommands->getFromDB($commands_id);
                     $item->getFromDB($items_id);
                     $targetParam = PluginShellcommandsShellcommand_Item::resolveLinkOfCommand($shellcommands->getID(), $item);
                     // Exec command on each targets : stop on first success
                     $selectedTarget = null;
                     if ($targetParam !== false) {
                         foreach ($targetParam as $target) {
                             list($error, $message) = PluginShellcommandsShellcommand_Item::execCommand($shellcommands->getID(), $target);
                             if (!$error) {
                                 $selectedTarget = $target;
                                 break;
                             }
                         }
                     }
                     echo "<tr class='tab_bg_1 shellcommands_result_line'>";
                     echo "<td class='center' colspan='4'>" . __($item->getType()) . ' : ' . $item->getLink() . " - " . $selectedTarget . "</td>";
                     echo "</tr>";
                     PluginShellcommandsShellcommand::displayCommandResult($shellcommands, $selectedTarget, $message, $error);
                 }
                 echo "</table>";
                 echo "</div>";
                 break;
         }
     }
 }