cmd_chain_array = array(); } /* enables log_error() of each command we run */ function setdebug() { $this->is_debugging = true; } /* no halt execution of CmdCHAIN if there is a failure */ function nohaltonerror() { $this->halt_on_errors = false; } /* halts execution of CmdCHAIN if there is a failure */ function sethaltonerror() { $this->halt_on_errors = true; } /* adds a command to the CmdCHAIN */ function add($cmd_title = "", $command = "", $ignore_return_text = false) { if(!$cmd_title) return; if(!$command) return; $temp = array(); $temp['cmd_title'] = $cmd_title; $temp['command'] = $command; if($ignore_return_text) $temp['ignore_return_text'] = true; else $temp['ignore_return_text'] = false; $this->cmd_chain_array[] = $temp; // add array to class return array(); } /* executes the CmdCHAIN one command at a time */ function execute() { foreach($this->cmd_chain_array as $cmd) { $cmd_title = $cmd['cmd_title']; $command = $cmd['command']; $ignore_return_text = $cmd['ignore_return_text']; // Should we perform verbose debugging? if($this->is_debugging == true) { log_error("CmdCHAIN is executing -> {$cmd_title} - {$command}"); usleep(100); // give network stack time to deliver network syslog message } // Execute command $status = exec($command); if($this->ignore_return_text == true) continue; if(intval($status) <> 0) { log_error("{$cmd_title} failed with return code -> {$status}. The command was {$command}"); if($this->halt_on_errors == true) return("{$cmd_title} failed with return code -> {$status}. The command was {$command}"); } } return; } } /* * example usage: * $cmdchain = new CmdCHAIN(); $cmdchain->add("grab freebsd version", "uname -a", false); $cmdchain->setdebug(); // optional for verbose logging $cmdchain->nohaltonerror(); // tells cmdchain to keep processing commands if any of them fail $cmdchain->execute(); $cmdchain->clear(); // clears the previous added entries */ ?>