Project

General

Profile

Download (5.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * diag_ping.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9
 * Copyright (c) 2003-2005 Bob Zoller (bob@kludgebox.com)
10
 * All rights reserved.
11
 *
12
 * originally based on m0n0wall (http://m0n0.ch/wall)
13
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
14
 * All rights reserved.
15
 *
16
 * Licensed under the Apache License, Version 2.0 (the "License");
17
 * you may not use this file except in compliance with the License.
18
 * You may obtain a copy of the License at
19
 *
20
 * http://www.apache.org/licenses/LICENSE-2.0
21
 *
22
 * Unless required by applicable law or agreed to in writing, software
23
 * distributed under the License is distributed on an "AS IS" BASIS,
24
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25
 * See the License for the specific language governing permissions and
26
 * limitations under the License.
27
 */
28

    
29
##|+PRIV
30
##|*IDENT=page-diagnostics-ping
31
##|*NAME=Diagnostics: Ping
32
##|*DESCR=Allow access to the 'Diagnostics: Ping' page.
33
##|*MATCH=diag_ping.php*
34
##|-PRIV
35

    
36
$allowautocomplete = true;
37
$pgtitle = array(gettext("Diagnostics"), gettext("Ping"));
38
require_once("guiconfig.inc");
39

    
40
define('MAX_COUNT', 10);
41
define('DEFAULT_COUNT', 3);
42
define('MAX_WAIT', 10);
43
define('DEFAULT_WAIT', 1);
44
$do_ping = false;
45
$host = '';
46
$count = DEFAULT_COUNT;
47
$wait = DEFAULT_WAIT;
48

    
49
if ($_POST || $_REQUEST['host']) {
50
	unset($input_errors);
51
	unset($do_ping);
52

    
53
	/* input validation */
54
	$reqdfields = explode(" ", "host count");
55
	$reqdfieldsn = array(gettext("Host"), gettext("Count"));
56
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
57
	if (($_REQUEST['count'] < 1) || ($_REQUEST['count'] > MAX_COUNT) || (!is_numericint($_REQUEST['wait']))) {
58
		$input_errors[] = sprintf(gettext("Count must be between 1 and %s"), MAX_COUNT);
59
	}	
60
	if (($_REQUEST['wait'] < 1) || ($_REQUEST['wait'] > MAX_WAIT) || (!is_numericint($_REQUEST['wait']))) {
61
		$input_errors[] = sprintf(gettext("Wait must be between 1 and %s"), MAX_WAIT);
62
	}	
63
	$host = trim($_REQUEST['host']);
64
	$ipproto = $_REQUEST['ipproto'];
65
	if (($ipproto == "ipv4") && is_ipaddrv6($host)) {
66
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
67
	}
68
	if (($ipproto == "ipv6") && is_ipaddrv4($host)) {
69
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
70
	}
71
	if (!is_ipaddr($host) && !is_hostname($host)) {
72
		$input_errors[] = gettext("Hostname must be a valid hostname or IP address.");
73
	}
74

    
75
	if (!$input_errors) {
76
		if ($_POST) {
77
			$do_ping = true;
78
		}
79
		if (isset($_REQUEST['sourceip'])) {
80
			$sourceip = $_REQUEST['sourceip'];
81
		}
82
		$count = (empty($_REQUEST['count'])) ? DEFAULT_WAIT : $_REQUEST['count'];
83
		$wait = (empty($_REQUEST['wait'])) ? DEFAULT_WAIT : $_REQUEST['wait'];
84
	}
85
}
86

    
87
if ($do_ping) {
88
?>
89
	<script type="text/javascript">
90
	//<![CDATA[
91
	window.onload=function() {
92
		document.getElementById("pingCaptured").wrap='off';
93
	}
94
	//]]>
95
	</script>
96
<?php
97
	$ifscope = '';
98
	$command = "/sbin/ping";
99
	if ($ipproto == "ipv6") {
100
		$command .= "6";
101
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
102
		if (is_linklocal($ifaddr)) {
103
			$ifscope = get_ll_scope($ifaddr);
104
		}
105
	} else {
106
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ip($sourceip);
107
	}
108

    
109
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
110
		$srcip = "-S" . escapeshellarg($ifaddr);
111
		if (is_linklocal($host) && !strstr($host, "%") && !empty($ifscope)) {
112
			$host .= "%{$ifscope}";
113
		}
114
	}
115

    
116
	$cmd = "{$command} {$srcip} -c" . escapeshellarg($count) . " -i" . escapeshellarg($wait) . " " . escapeshellarg($host);
117
	//echo "Ping command: {$cmd}\n";
118
	$result = shell_exec($cmd);
119

    
120
	if (empty($result)) {
121
		$input_errors[] = sprintf(gettext('Host "%s" did not respond or could not be resolved.'), $host);
122
	}
123

    
124
}
125

    
126
include('head.inc');
127

    
128
if ($input_errors) {
129
	print_input_errors($input_errors);
130
}
131

    
132
$form = new Form(false);
133

    
134
$section = new Form_Section('Ping');
135

    
136
$section->addInput(new Form_Input(
137
	'host',
138
	'*Hostname',
139
	'text',
140
	$host,
141
	['placeholder' => 'Hostname to ping']
142
));
143

    
144
$section->addInput(new Form_Select(
145
	'ipproto',
146
	'*IP Protocol',
147
	$ipproto,
148
	['ipv4' => 'IPv4', 'ipv6' => 'IPv6']
149
));
150

    
151
$section->addInput(new Form_Select(
152
	'sourceip',
153
	'*Source address',
154
	$sourceip,
155
	array('' => gettext('Automatically selected (default)')) + get_possible_traffic_source_addresses(true)
156
))->setHelp('Select source address for the ping.');
157

    
158
$section->addInput(new Form_Select(
159
	'count',
160
	'Maximum number of pings',
161
	$count,
162
	array_combine(range(1, MAX_COUNT), range(1, MAX_COUNT))
163
))->setHelp('Select the maximum number of pings.');
164

    
165
$section->addInput(new Form_Select(
166
	'wait',
167
	'Seconds between pings',
168
	$wait,
169
	array_combine(range(1, MAX_WAIT), range(1, MAX_WAIT))
170
))->setHelp('Select the number of seconds to wait between pings.');
171

    
172
$form->add($section);
173

    
174
$form->addGlobal(new Form_Button(
175
	'Submit',
176
	'Ping',
177
	null,
178
	'fa-rss'
179
))->addClass('btn-primary');
180

    
181
print $form;
182

    
183
if ($do_ping && !empty($result) && !$input_errors) {
184
?>
185
	<div class="panel panel-default">
186
		<div class="panel-heading">
187
			<h2 class="panel-title"><?=gettext('Results')?></h2>
188
		</div>
189

    
190
		<div class="panel-body">
191
			<pre><?= htmlspecialchars($result) ?></pre>
192
		</div>
193
	</div>
194
<?php
195
}
196

    
197
include('foot.inc');
(25-25/227)