From 75fa1a6e88ef2cc1d9572788152e025fc5305f1b Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 4 May 2016 13:14:33 +0200 Subject: [PATCH] --- ping.php | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/ping.php b/ping.php index ca5a562..f1df4b1 100644 --- a/ping.php +++ b/ping.php @@ -3,6 +3,16 @@ * Pings a given host and returns a red or green image. * Green when the host is online, red when offline. * + * Error: ping: socket: Operation not permitted + * (raw socket required by specified options). + * + * Solution: + * $ chmod +s /path/to/ping + * Alternative: + * $ setcap cap_net_raw+ep /path/to/ping + * Alternative: + * make it passwordless sudo + * * Keywords: http image ping online check * * @author Christian Weiske @@ -21,13 +31,18 @@ if (!isset($_GET['size'])) { $size = intval($_GET['size']); } -exec('ping -c 1 -w 1 -W 1 ' . escapeshellarg($host), $output, $retval); +exec('ping -c 1 -w 1 -W 1 ' . escapeshellarg($host) . ' 2>&1', $output, $retval); if ($retval == 0) { //all fine sendSvg($size, 'fill: green'); -} else { - //error +} else if ($retval == 1) { + //no reply sendSvg($size, 'fill: red'); +} else { + //some other error, e.g. permissions + header('HTTP/1.0 500 Internal server error'); + header('Content-type: text/plain'); + echo implode("\n", $output) . "\n"; } function sendSvg($size, $style) @@ -35,8 +50,8 @@ function sendSvg($size, $style) $half = intval($size / 2); header('HTTP/1.0 200 OK'); header('Content-type: image/svg+xml'); + echo '<' . '?xml version="1.0"?>' . "\n"; echo << -- 2.30.2