Project

General

Profile

Download (9.51 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php -f
2
 
3
<?php
4

    
5
require_once("globals.inc");
6
echo "Starting the {$g['product_name']} developer shell";
7
require_once("functions.inc");
8
echo ".";
9
require_once("config.inc");
10
echo ".";
11
require_once("util.inc");
12
echo ".";
13

    
14
$shell_cmds = array("alias", "alloc", "bg", "bind", "bindkey", "break", 
15
     "breaksw", "builtins", "case", "cd", "chdir", "command", "complete", "continue", "default",
16
     "dirs", "do", "done", "echo", "echotc", "elif", "else", "end", "endif", "endsw", "esac", "eval",
17
     "exec", "exit", "export", "false", "fc", "fg", "filetest", "fi", "for", "foreach", "getopts",
18
     "glob", "goto", "hash", "hashstat", "history", "hup", "if", "jobid", "jobs", "kill", "limit",
19
     "local", "log", "login", "logout", "ls-F", "nice", "nohup", "notify", "onintr", "popd",
20
     "printenv", "pushd", "pwd", "read", "readonly", "rehash", "repeat", "return", "sched", "set",
21
     "setenv", "settc", "setty", "setvar", "shift", "source", "stop", "suspend", "switch",
22
     "telltc", "test", "then", "time", "trap", "true", "type", "ulimit", "umask", "unalias",
23
     "uncomplete", "unhash", "unlimit", "unset", "unsetenv", "until", "wait", "where", "which",
24
     "while");
25

    
26
function pipe_cmd($command, $text_to_pipe) {
27
	$descriptorspec = array(
28
	    0 => array("pipe", "r"),  // stdin
29
	    1 => array("pipe", "w"),  // stdout
30
	    2 => array("pipe", "w")); // stderr ?? instead of a file
31
	
32
	$fd = proc_open("$command", $descriptorspec, $pipes);
33
	if (is_resource($fd)) {
34
	        fwrite($pipes[0], "{$text_to_pipe}");
35
	        fclose($pipes[0]);
36
	        while($s= fgets($pipes[1], 1024)) {
37
	          // read from the pipe
38
	          $buffer .= $s;
39
	        }
40
	        fclose($pipes[1]);
41
	        fclose($pipes[2]);
42
	}
43
	return $buffer;
44
}
45

    
46
if(!function_exists("readline")) {
47
	function readline() {
48
		$fp = fopen('php://stdin', 'r');
49
		$textinput = chop(fgets($fp));
50
		fclose($fp);
51
		return $textinput;
52
	}
53
}
54

    
55
function more($text, $count=24) {
56
        $counter=0;
57
        $lines = explode("\n", $text);
58
        foreach($lines as $line) {
59
                if($counter > $count) {
60
                        echo "Press RETURN to continue ...";
61
                        $fp = fopen('php://stdin', 'r');
62
                        $pressreturn = chop(fgets($fp));
63
                        if($pressreturn == "q" || $pressreturn == "quit") 
64
                        	return; 
65
                        fclose($fp);
66
                        $counter = 0;
67
                }
68
                echo "{$line}\n";
69
                $counter++;
70
        }
71
}
72

    
73
function show_help() {
74

    
75
$show_help_text = <<<EOF
76

    
77
	Enter a series of commands and then execute the set with "exec".
78
	
79
	For example:
80
	echo "foo"; // php command
81
	echo "foo2"; // php command
82
	! echo "heh" # shell command
83
	exec
84

    
85
	Example commands:
86

    
87
	startrecording <recordingfilename>
88
	stoprecording <recordingfilename>
89
	showrecordings
90

    
91
	parse_config(true);  # reloads the \$config array
92

    
93
	\$temp = print_r(\$config, true);
94
	more(\$temp);
95

    
96
	/* to output a configuration array */
97
	print_r(\$config);
98
	
99
	/* to output the interfaces configuration portion of config.xml */
100
	print_r(\$config['interfaces']);
101
	
102
	/* to output the dhcp server configuration */
103
	print_r(\$config['dhcpd']);
104

    
105
	/* to exit the {$g['product_name']} developer shell */
106
	exit
107
	
108
	/* to output supported wireless modes for an interface */
109
	print_r(get_wireless_modes(\"ath0\"));
110
	
111
	/* to enable SSH */
112
	\$config['system']['enablesshd'] = true;
113
	
114
	/* change OPTX to the OPT interface name such as BACKHAUL */
115
	\$config['interfaces']['optx']['wireless']['standard'] = "11a";
116
	\$config['interfaces']['optx']['wireless']['mode'] = "hostap";
117
	\$config['interfaces']['optx']['wireless']['channel'] = "6";
118
	
119
	/* to enable dhcp server for an optx interface */
120
	\$config['dhcpd']['optx']['enable'] = true;
121
	\$config['dhcpd']['optx']['range']['from'] = "192.168.31.100";
122
	\$config['dhcpd']['optx']['range']['to'] = "192.168.31.150";
123
	
124
	/* to disable the firewall filter */
125
	\$config['system']['disablefilter'] = true;
126
	
127
	/* to enable an interface and configure it as a DHCP client */
128
	\$config['interfaces']['optx']['disabled'] = false;
129
	\$config['interfaces']['optx']['ipaddr'] = "dhcp";
130
	
131
	/* to enable an interface and set a static IPv4 address */
132
	\$config['interfaces']['wan']['enable'] = true;
133
	\$config['interfaces']['wan']['ipaddr'] = "192.168.100.1";
134
	\$config['interfaces']['wan']['subnet'] = "24";
135
	
136
	/* to save out the new configuration (config.xml) */
137
	write_config();
138
	
139
	/* to reboot the system after saving */
140
	system_reboot_sync();
141
	
142
EOF;
143

    
144
	more($show_help_text);
145
 	
146
}
147

    
148
$fp = fopen('php://stdin', 'r');
149

    
150
echo ".\n\n";
151

    
152
$pkg_interface='console';
153

    
154
$shell_active = true;
155
$tccommands = array();
156

    
157
function completion($string, $index) {
158
	global $tccommands;
159
	return $tccommands;
160
}
161

    
162
readline_completion_function("completion");
163

    
164
if($argc < 2) {
165
	echo "Welcome to the {$g['product_name']} developer shell\n";
166
	echo "\nType \"help\" to show common usage scenarios.\n";
167
	echo "\nAvailable playback commands:\n     ";
168
	$files = scandir("/etc/phpshellsessions/");
169
	$tccommands[] = "playback";
170
	foreach($files as $file) {
171
		if($file <> "." and $file <> "..") {
172
			echo $file . " ";
173
			if(function_exists("readline_add_history")) {
174
				readline_add_history("playback $file");
175
				$tccommands[] = "$file";
176
			}
177
		}
178
	}
179
	echo "\n\n";
180
}
181

    
182
$recording = false;
183
$playback_file_split = array();
184
$playbackbuffer = "";
185

    
186
if($argv[1]=="playback" or $argv[1]=="run") { 
187
	if(!file_exists("/etc/phpshellsessions/{$argv[2]}")) {
188
		echo "Could not locate playback file.";
189
		exit;
190
	}
191
	playback_file($argv[2]);
192
	exit;
193
}
194

    
195
// Define more commands
196
$tccommands[] = "exit";
197
$tccommands[] = "quit";
198
$tccommands[] = "?";
199
$tccommands[] = "exec";
200
$tccommands[] = "startrecording";
201
$tccommands[] = "stoprecording";
202
$tccommands[] = "showrecordings";
203
$tccommands[] = "record";
204
$tccommands[] = "reset";
205
$tccommands[] = "master";
206
$tccommands[] = "RELENG_1_2";
207

    
208
while($shell_active == true) {
209
	$command = readline("{$g['product_name']} shell: ");
210
	readline_add_history($command);
211
    $command_split = explode(" ", $command);
212
    $first_command = $command_split[0];	
213
	if($first_command == "playback" || $first_command == "run") {
214
		$playback_file = $command_split[1];
215
		if(!$playback_file || !file_exists("/etc/phpshellsessions/{$playback_file}")) {
216
			$command = "";
217
			echo "Could not locate playback file.\n";
218
		} else {
219
			$command = "";
220
			echo "\nPlayback of file {$command_split[1]} started.\n\n";
221
			playback_file("{$playback_file}");
222
			continue;
223
		}
224
	}
225
	if($first_command == "exit" or $first_command == "quit") 
226
		die;
227
	if($first_command == "help" or $first_command == "?") {
228
		show_help();
229
		$playbackbuffer = "";
230
		continue;
231
	}
232
	if($first_command == "exec" or $first_command == "exec;") {
233
		playback_text($playbackbuffer);
234
		$playbackbuffer = "";
235
		continue;
236
	}
237
	if($first_command == "stoprecording" || $first_command == "stoprecord" || $first_command == "stop") {
238
		if($recording) {
239
			fwrite($recording_fd, $playbackbuffer);
240
			fclose($recording_fd);
241
			$command = "";
242
			conf_mount_ro();
243
			echo "Recording stopped.\n";
244
			$recording = false; 
245
		} else {
246
			echo "No recording session in progress.\n";
247
			$command = "";
248
		}
249
	}
250
	if($first_command == "showrecordings") {
251
		show_recordings();
252
		$command = "";		
253
	}
254
	if($first_command == "reset") {
255
		$playbackbuffer = "";
256
		echo "\nBuffer reset.\n\n";
257
		continue;
258
	}
259
	if($first_command == "record") {
260
		if(!$command_split[1]) {
261
			echo "usage: record playbackname\n";
262
			$command = "";
263
		} else {
264
			/* time to record */
265
			conf_mount_rw();
266
			safe_mkdir("/etc/phpshellsessions");
267
			$recording_fd = fopen("/etc/phpshellsessions/{$command_split[1]}","w");
268
			if(!$recording_fd) {
269
				echo "Could not start recording session.\n";
270
				$command = "";
271
			} else { 
272
				$recording = true;
273
				echo "Recording of {$command_split[1]} started.\n";
274
				$command = "";
275
			}
276
		}
277
	}
278
	$playbackbuffer .= $command . "\n";
279
}
280

    
281
function show_recordings() {
282
	conf_mount_rw();
283
	safe_mkdir("/etc/phpshellsessions");
284
	if($recording) 
285
		conf_mount_ro();
286
	echo "==> Sessions available for playback are:\n";
287
	system("cd /etc/phpshellsessions && ls /etc/phpshellsessions");
288
	echo "==> end of list.\n";	
289
}
290

    
291
function returnlastchar($command) {
292
	$commandlen = strlen($command);
293
	$endofstring = substr($command, ($commandlen-1));
294
	return $endofstring; 
295
}
296

    
297
function returnfirstchar($command) {
298
	$commandlen = strlen($command);
299
	$endofstring = substr($command, 0, 1);
300
	return $endofstring; 
301
}
302

    
303
function str_replace_all($search,$replace,$subject) {
304
	while(strpos($subject,$search)!==false) 
305
		$subject = str_replace($search,$replace,$subject);
306
	return $subject;
307
}
308

    
309
function playback_text($playback_file_contents) {
310
	$playback_file_split = explode("\n", $playback_file_contents);
311
	$playback_text  = "require_once('functions.inc');\n";
312
	$playback_text .= "require_once('globals.inc');\n";
313
	$playback_text .= "require_once('config.inc');\n";
314
	$toquote = '"';
315
	$toquotereplace = '\\"';	
316
	foreach($playback_file_split as $pfs) {
317
		$firstchar = returnfirstchar($pfs);
318
		$currentline = $pfs;
319
		if($firstchar == "!") {
320
			/* XXX: encode " in $pfs */
321
			$pfsa = str_replace($toquote, $toquotereplace, $currentline);
322
			$playback_text .= str_replace("!", "system(\"", $pfsa) . "\");\n";
323
		} else if ($firstchar == "=") {
324
			/* XXX: encode " in $pfs */
325
			$pfsa = str_replace($toquote, $toquotereplace, $currentline);
326
			$currentline   .= str_replace("!", "system(\"", $pfsa) . "\");\n";
327
		} else {
328
			$playback_text .= $pfs . "\n";
329
		}
330
	}
331
	global $config;
332
	eval($playback_text);
333
}
334

    
335
function playback_file($playback_file) {
336
	$playback_file_contents = file_get_contents("/etc/phpshellsessions/{$playback_file}");
337
	playback_text($playback_file_contents);
338
}
339

    
340
?>
(11-11/21)