Project

General

Profile

Download (4.5 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2
/*
3 c5d81585 Renato Botelho
 * diag_ping.php
4 9da2cf1c Stephen Beaver
 *
5 c5d81585 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6 81299b5c Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 c5d81585 Renato Botelho
 * Copyright (c) 2003-2005 Bob Zoller (bob@kludgebox.com)
8
 * All rights reserved.
9 fd9ebcd5 Stephen Beaver
 *
10 c5d81585 Renato Botelho
 * originally based on m0n0wall (http://m0n0.ch/wall)
11
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
12
 * All rights reserved.
13 fd9ebcd5 Stephen Beaver
 *
14 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
15
 * you may not use this file except in compliance with the License.
16
 * You may obtain a copy of the License at
17 fd9ebcd5 Stephen Beaver
 *
18 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
19 fd9ebcd5 Stephen Beaver
 *
20 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
21
 * distributed under the License is distributed on an "AS IS" BASIS,
22
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23
 * See the License for the specific language governing permissions and
24
 * limitations under the License.
25 fd9ebcd5 Stephen Beaver
 */
26 5b237745 Scott Ullrich
27 6b07c15a Matthew Grooms
##|+PRIV
28
##|*IDENT=page-diagnostics-ping
29 5230f468 jim-p
##|*NAME=Diagnostics: Ping
30 6b07c15a Matthew Grooms
##|*DESCR=Allow access to the 'Diagnostics: Ping' page.
31
##|*MATCH=diag_ping.php*
32
##|-PRIV
33
34 0d56c06b jim-p
$allowautocomplete = true;
35 36678039 Carlos Eduardo Ramos
$pgtitle = array(gettext("Diagnostics"), gettext("Ping"));
36 53483463 jim-p
require_once("guiconfig.inc");
37
38 5b237745 Scott Ullrich
define('MAX_COUNT', 10);
39
define('DEFAULT_COUNT', 3);
40 ed69a496 jim-p
$do_ping = false;
41
$host = '';
42
$count = DEFAULT_COUNT;
43 5b237745 Scott Ullrich
44 b8cc74ed sullrich
if ($_POST || $_REQUEST['host']) {
45 5b237745 Scott Ullrich
	unset($input_errors);
46
	unset($do_ping);
47
48
	/* input validation */
49
	$reqdfields = explode(" ", "host count");
50 699737d9 Phil Davis
	$reqdfieldsn = array(gettext("Host"), gettext("Count"));
51 1e9b4611 Renato Botelho
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
52 5b237745 Scott Ullrich
53 b8cc74ed sullrich
	if (($_REQUEST['count'] < 1) || ($_REQUEST['count'] > MAX_COUNT)) {
54 4a5430e3 cadu
		$input_errors[] = sprintf(gettext("Count must be between 1 and %s"), MAX_COUNT);
55 5b237745 Scott Ullrich
	}
56
57 4f3bb062 jim-p
	$host = trim($_REQUEST['host']);
58
	$ipproto = $_REQUEST['ipproto'];
59 5f601060 Phil Davis
	if (($ipproto == "ipv4") && is_ipaddrv6($host)) {
60 4f3bb062 jim-p
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
61 5f601060 Phil Davis
	}
62
	if (($ipproto == "ipv6") && is_ipaddrv4($host)) {
63 4f3bb062 jim-p
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
64 5f601060 Phil Davis
	}
65 4f3bb062 jim-p
66 5b237745 Scott Ullrich
	if (!$input_errors) {
67 ed69a496 jim-p
		if ($_POST) {
68
			$do_ping = true;
69
		}
70 947141fd Phil Davis
		if (isset($_REQUEST['sourceip'])) {
71 5e1cfe9e heper
			$sourceip = $_REQUEST['sourceip'];
72
		}
73
		$count = $_REQUEST['count'];
74 699737d9 Phil Davis
		if (preg_match('/[^0-9]/', $count)) {
75 3a134b53 Erik Fonnesbeck
			$count = DEFAULT_COUNT;
76 5f601060 Phil Davis
		}
77 5b237745 Scott Ullrich
	}
78
}
79 45d6ada5 Sjon Hortensius
80 288a2a0f Phil Davis
if ($do_ping) {
81 f1a9b09d Colin Fleming
?>
82 45d6ada5 Sjon Hortensius
	<script type="text/javascript">
83
	//<![CDATA[
84 947141fd Phil Davis
	window.onload=function() {
85 45d6ada5 Sjon Hortensius
		document.getElementById("pingCaptured").wrap='off';
86
	}
87
	//]]>
88
	</script>
89 f1a9b09d Colin Fleming
<?php
90 45d6ada5 Sjon Hortensius
	$ifscope = '';
91
	$command = "/sbin/ping";
92
	if ($ipproto == "ipv6") {
93
		$command .= "6";
94
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
95 947141fd Phil Davis
		if (is_linklocal($ifaddr)) {
96 45d6ada5 Sjon Hortensius
			$ifscope = get_ll_scope($ifaddr);
97 947141fd Phil Davis
		}
98 45d6ada5 Sjon Hortensius
	} else {
99
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ip($sourceip);
100
	}
101
102
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
103
		$srcip = "-S" . escapeshellarg($ifaddr);
104 947141fd Phil Davis
		if (is_linklocal($host) && !strstr($host, "%") && !empty($ifscope)) {
105 45d6ada5 Sjon Hortensius
			$host .= "%{$ifscope}";
106 947141fd Phil Davis
		}
107 3c98b3f7 jim-p
	}
108 45d6ada5 Sjon Hortensius
109
	$cmd = "{$command} {$srcip} -c" . escapeshellarg($count) . " " . escapeshellarg($host);
110
	//echo "Ping command: {$cmd}\n";
111
	$result = shell_exec($cmd);
112
113 947141fd Phil Davis
	if (empty($result)) {
114 babf5d85 Phil Davis
		$input_errors[] = sprintf(gettext('Host "%s" did not respond or could not be resolved.'), $host);
115 947141fd Phil Davis
	}
116 45d6ada5 Sjon Hortensius
117
}
118
119
include('head.inc');
120
121 947141fd Phil Davis
if ($input_errors) {
122 45d6ada5 Sjon Hortensius
	print_input_errors($input_errors);
123 947141fd Phil Davis
}
124 45d6ada5 Sjon Hortensius
125 37676f4e jim-p
$form = new Form(false);
126 45d6ada5 Sjon Hortensius
127
$section = new Form_Section('Ping');
128
129
$section->addInput(new Form_Input(
130
	'host',
131 3e2028f4 Phil Davis
	'*Hostname',
132 45d6ada5 Sjon Hortensius
	'text',
133
	$host,
134
	['placeholder' => 'Hostname to ping']
135
));
136
137 214e79b0 Stephen Beaver
$section->addInput(new Form_Select(
138 45d6ada5 Sjon Hortensius
	'ipproto',
139 fe54f091 Phil Davis
	'*IP Protocol',
140 214e79b0 Stephen Beaver
	$ipproto,
141
	['ipv4' => 'IPv4', 'ipv6' => 'IPv6']
142
));
143 45d6ada5 Sjon Hortensius
144
$section->addInput(new Form_Select(
145
	'sourceip',
146 fe54f091 Phil Davis
	'*Source address',
147 3f91d34e Stephen Beaver
	$sourceip,
148 430cee65 stilez
	array('' => gettext('Automatically selected (default)')) + get_possible_traffic_source_addresses(true)
149 3728bbe5 NOYB
))->setHelp('Select source address for the ping.');
150 45d6ada5 Sjon Hortensius
151
$section->addInput(new Form_Select(
152
	'count',
153
	'Maximum number of pings',
154
	$count,
155
	array_combine(range(1, MAX_COUNT), range(1, MAX_COUNT))
156 3728bbe5 NOYB
))->setHelp('Select the maximum number of pings.');
157 45d6ada5 Sjon Hortensius
158
$form->add($section);
159 37676f4e jim-p
160
$form->addGlobal(new Form_Button(
161
	'Submit',
162 faab522f Renato Botelho
	'Ping',
163 37676f4e jim-p
	null,
164
	'fa-rss'
165
))->addClass('btn-primary');
166
167 45d6ada5 Sjon Hortensius
print $form;
168
169 288a2a0f Phil Davis
if ($do_ping && !empty($result) && !$input_errors) {
170 45d6ada5 Sjon Hortensius
?>
171
	<div class="panel panel-default">
172
		<div class="panel-heading">
173 babf5d85 Phil Davis
			<h2 class="panel-title"><?=gettext('Results')?></h2>
174 45d6ada5 Sjon Hortensius
		</div>
175
176
		<div class="panel-body">
177
			<pre><?= $result ?></pre>
178
		</div>
179
	</div>
180
<?php
181
}
182
183
include('foot.inc');