Project

General

Profile

Download (3.47 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3
/*
4
	cmd_chain.inc
5
	Part of pfSense
6
	Copyright (C) 2008 Scott Ullrich
7
	All rights reserved.
8

    
9
	Redistribution and use in source and binary forms, with or without
10
	modification, are permitted provided that the following conditions are met:
11

    
12
	1. Redistributions of source code must retain the above copyright notice,
13
	   this list of conditions and the following disclaimer.
14

    
15
	2. Redistributions in binary form must reproduce the above copyright
16
	   notice, this list of conditions and the following disclaimer in the
17
	   documentation and/or other materials provided with the distribution.
18

    
19
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
	POSSIBILITY OF SUCH DAMAGE.
29
*/
30

    
31
/* 
32
 * HANDY FOR STAND ALONE DEBUGGING OF CLASS. 
33
 *
34

    
35
	function log_error($text) {
36
		echo $text . "\n";
37
	}
38

    
39
*/
40

    
41
/*
42
 *   CmdCHAIN allows a chaining of commands into one call.
43
 *   If debugging is enabled verbose logging is applied.
44
 *   During the command(s) execution if it fails the result
45
 *   will be reported to syslog so that the problem can be reported.
46
 */
47
Class CmdCHAIN {
48

    
49
		var $cmd_chain_array = array();
50
		var $is_debugging;
51
		
52
		/* clear() erases the current cmdchain */
53
		function clear() {
54
			unset($cmd_chain_array);
55
			$this->cmd_chain_array = array();
56
		}
57
		
58
		/* enables log_error() of each command we run */
59
		function setdebug() {
60
			$this->is_debugging = true;
61
		}
62

    
63
		/* adds a command to the CmdCHAIN */
64
		function add($cmd_title = "", $command = "", $ignore_return_text = false) {
65
			if(!$cmd_title) 
66
				return;
67
			if(!$command) 
68
				return;
69
			$temp = array();
70
			$temp['cmd_title'] = $cmd_title;
71
			$temp['command'] = $command;
72
			if($ignore_return_text)
73
				$temp['ignore_return_text'] = true;
74
			else 
75
				$temp['ignore_return_text'] = false;
76
			$this->cmd_chain_array[] = $temp; // add array to class
77
			return array();
78
		}
79
		
80
		/* executes the CmdCHAIN one command at a time */
81
		function execute() {
82
			foreach($this->cmd_chain_array as $cmd) {
83
				$cmd_title = $cmd['cmd_title'];
84
				$command = $cmd['command'];
85
				$ignore_return_text = $cmd['ignore_return_text'];
86
				// Should we perform verbose debugging?
87
				if($this->is_debugging == true) {
88
					log_error("CmdCHAIN is executing -> {$cmd_title} - {$command}");
89
					usleep(100);	// give network stack time to deliver network syslog message
90
				}
91
				// Execute command
92
				$status = exec($command);
93
				if(!$status and $this->ignore_return_text == false) {
94
					log_error("{$cmd_title} failed with return code -> {$status}.  The command was {$command}");
95
					return("{$cmd_title} failed with return code -> {$status}");
96
				} else {
97
					if($this->is_debugging == true) {
98
						log_error("{$cmd_title} returned -> {$status}");
99
					}
100
				}
101
			}
102
			return;
103
		}
104
       
105
}
106

    
107
/* 
108
 * example usage:
109
 *
110

    
111
$cmdchain = new CmdCHAIN();
112
$cmdchain->add("grab freebsd version", "uname -a", false);
113
$cmdchain->setdebug(); // optional for verbose logging
114
$cmdchain->execute();
115

    
116
*/
117

    
118
?>
(6-6/31)