Projet

Général

Profil

« Précédent | Suivant » 

Révision 017e4ad3

Ajouté par jim-p il y a presque 10 ans

Add some rudimentary and/or logic to the host field of packet capture.

Voir les différences:

usr/local/www/diag_packet_capture.php
37 37

  
38 38
$allowautocomplete = true;
39 39

  
40
function fixup_host_logic($value) {
41
	return str_replace(array(" ", ",", "+", "|", "!"), array("", "and ", "and ", "or ", "not "), $value);
42
}
43
function strip_host_logic($value) {
44
	return str_replace(array(" ", ",", "+", "|", "!"), array("", "", "", "", ""), $value);
45
}
46
function get_host_boolean($value, $host) {
47
	$value = str_replace(array("!", $host), array("", ""), $value);
48
	$andor = "";
49
	switch (trim($value)) {
50
		case "|":
51
			$andor = "or ";
52
			break;
53
		case ",":
54
		case "+":
55
			$andor = "and ";
56
			break;
57
	}
58
	return $andor;
59
}
60
function has_not($value) {
61
	return strpos($value, '!') !== false;
62
}
40 63
function fixup_not($value) {
41 64
	return str_replace("!", "not ", $value);
42 65
}
43 66
function strip_not($value) {
44
	return ltrim($value, '!');
67
	return ltrim(trim($value), '!');
68
}
69

  
70
function fixup_host($value, $position) {
71
	$host = strip_host_logic($value);
72
	$not = has_not($value) ? "not " : "";
73
	$andor = ($position > 0) ? get_host_boolean($value, $host) : "";
74
	if (is_ipaddr($host))
75
		return "{$andor}host {$not}" . $host;
76
	elseif (is_subnet($host))
77
		return "{$andor}net {$not}" . $host;
78
	else
79
		return "";
45 80
}
46 81

  
47 82
if ($_POST['downloadbtn'] == gettext("Download Capture"))
......
96 131
	}
97 132
	
98 133
	if ($host != "") {
99
		if (!is_subnet(strip_not($host)) && !is_ipaddr(strip_not($host))) {
100
			$input_errors[] = sprintf(gettext("A valid IP address or CIDR block must be specified. [%s]"), $host);
134
		$host_string = str_replace(array(" ", "|", ","), array("", "#|", "#+"), $host);
135
		if (strpos($host_string, '#') === false) {
136
			$hosts = array($host);
137
		} else {
138
			$hosts = explode('#', $host_string);
139
		}
140
		foreach ($hosts as $h) {
141
			if (!is_subnet(strip_host_logic($h)) && !is_ipaddr(strip_host_logic($h))) {
142
				$input_errors[] = sprintf(gettext("A valid IP address or CIDR block must be specified. [%s]"), $h);
143
			}
101 144
		}
102 145
	}
103 146
	if ($port != "") {
......
352 395
				$matches[] = "port ".fixup_not($port);
353 396

  
354 397
			if ($host != "") {
355
				if (is_ipaddr($host))
356
					$matches[] = "host " . fixup_not($host);
357
				elseif (is_subnet($host))
358
					$matches[] = "net " . fixup_not($host);
398
				$hostmatch = "";
399
				$hostcount = 0;
400
				foreach ($hosts as $h) {
401
					$h = fixup_host($h, $hostcount++);
402
					if (!empty($h))
403
						$hostmatch .= " " . $h;
404
				}
405
				if (!empty($hostmatch))
406
					$matches[] = "({$hostmatch})";
359 407
			}
360 408

  
361 409
			if ($count != "0" ) {
......
369 417
			if ($action == gettext("Start")) {
370 418
				$matchstr = implode($matches, " and ");
371 419
				echo("<strong>" . gettext("Packet Capture is running.") . "</strong><br />");
372
				mwexec_bg ("/usr/sbin/tcpdump -i {$selectedif} {$disablepromiscuous} {$searchcount} -s {$snaplen} -w {$fp}{$fn} {$matchstr}");
420
				$cmd = "/usr/sbin/tcpdump -i {$selectedif} {$disablepromiscuous} {$searchcount} -s {$snaplen} -w {$fp}{$fn} " . escapeshellarg($matchstr);
421
				// Debug
422
				//echo $cmd;
423
				mwexec_bg ($cmd);
373 424
			} else {
374 425
				//action = stop
375 426
				echo("<strong>" . gettext("Packet Capture stopped.") . "<br /><br />" . gettext("Packets Captured:") . "</strong><br />");

Formats disponibles : Unified diff