Project

General

Profile

Download (4.59 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-2019 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
$do_ping = false;
43
$host = '';
44
$count = DEFAULT_COUNT;
45

    
46
if ($_POST || $_REQUEST['host']) {
47
	unset($input_errors);
48
	unset($do_ping);
49

    
50
	/* input validation */
51
	$reqdfields = explode(" ", "host count");
52
	$reqdfieldsn = array(gettext("Host"), gettext("Count"));
53
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
54

    
55
	if (($_REQUEST['count'] < 1) || ($_REQUEST['count'] > MAX_COUNT)) {
56
		$input_errors[] = sprintf(gettext("Count must be between 1 and %s"), MAX_COUNT);
57
	}
58

    
59
	$host = trim($_REQUEST['host']);
60
	$ipproto = $_REQUEST['ipproto'];
61
	if (($ipproto == "ipv4") && is_ipaddrv6($host)) {
62
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
63
	}
64
	if (($ipproto == "ipv6") && is_ipaddrv4($host)) {
65
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
66
	}
67

    
68
	if (!$input_errors) {
69
		if ($_POST) {
70
			$do_ping = true;
71
		}
72
		if (isset($_REQUEST['sourceip'])) {
73
			$sourceip = $_REQUEST['sourceip'];
74
		}
75
		$count = $_REQUEST['count'];
76
		if (preg_match('/[^0-9]/', $count)) {
77
			$count = DEFAULT_COUNT;
78
		}
79
	}
80
}
81

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

    
104
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
105
		$srcip = "-S" . escapeshellarg($ifaddr);
106
		if (is_linklocal($host) && !strstr($host, "%") && !empty($ifscope)) {
107
			$host .= "%{$ifscope}";
108
		}
109
	}
110

    
111
	$cmd = "{$command} {$srcip} -c" . escapeshellarg($count) . " " . escapeshellarg($host);
112
	//echo "Ping command: {$cmd}\n";
113
	$result = shell_exec($cmd);
114

    
115
	if (empty($result)) {
116
		$input_errors[] = sprintf(gettext('Host "%s" did not respond or could not be resolved.'), $host);
117
	}
118

    
119
}
120

    
121
include('head.inc');
122

    
123
if ($input_errors) {
124
	print_input_errors($input_errors);
125
}
126

    
127
$form = new Form(false);
128

    
129
$section = new Form_Section('Ping');
130

    
131
$section->addInput(new Form_Input(
132
	'host',
133
	'*Hostname',
134
	'text',
135
	$host,
136
	['placeholder' => 'Hostname to ping']
137
));
138

    
139
$section->addInput(new Form_Select(
140
	'ipproto',
141
	'*IP Protocol',
142
	$ipproto,
143
	['ipv4' => 'IPv4', 'ipv6' => 'IPv6']
144
));
145

    
146
$section->addInput(new Form_Select(
147
	'sourceip',
148
	'*Source address',
149
	$sourceip,
150
	array('' => gettext('Automatically selected (default)')) + get_possible_traffic_source_addresses(true)
151
))->setHelp('Select source address for the ping.');
152

    
153
$section->addInput(new Form_Select(
154
	'count',
155
	'Maximum number of pings',
156
	$count,
157
	array_combine(range(1, MAX_COUNT), range(1, MAX_COUNT))
158
))->setHelp('Select the maximum number of pings.');
159

    
160
$form->add($section);
161

    
162
$form->addGlobal(new Form_Button(
163
	'Submit',
164
	'Ping',
165
	null,
166
	'fa-rss'
167
))->addClass('btn-primary');
168

    
169
print $form;
170

    
171
if ($do_ping && !empty($result) && !$input_errors) {
172
?>
173
	<div class="panel panel-default">
174
		<div class="panel-heading">
175
			<h2 class="panel-title"><?=gettext('Results')?></h2>
176
		</div>
177

    
178
		<div class="panel-body">
179
			<pre><?= $result ?></pre>
180
		</div>
181
	</div>
182
<?php
183
}
184

    
185
include('foot.inc');
(24-24/225)