Skip to content

Instantly share code, notes, and snippets.

@geerlingguy
Last active March 4, 2021 22:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geerlingguy/4675221 to your computer and use it in GitHub Desktop.
Save geerlingguy/4675221 to your computer and use it in GitHub Desktop.
Simple script to use the Ping PHP library and print a list of servers and their statuses. Written for a school that needs to track whether some local Apple TVs are active.
<?php
/**
* @file
* Show the status of various Apple TVs.
*/
// Place Ping.php in the same folder as this script.
require_once('Ping.php');
use JJG\Ping as Ping;
// Define a list of servers to check. Each item in the array is one server; the
// key is the title of the server, and the value is the ip address or host name
// that will be checked.
$servers_to_check = array(
'Google' => 'www.google.com',
'Apple' => 'www.apple.com',
'Yahoo' => 'www.yahoo.com',
'Apple TV - Room 201' => '192.168.1.123', // Define as many as you'd like.
);
// Print a tite for the page.
print '<h1 class="title">' . 'Server Status Page' . '</h1>';
// Begin an unordered list.
print '<ul class="server-list">' . "\r\n";
// Loop through the servers defined above, and print the status of each.
foreach ($servers_to_check as $name => $address) {
// Begin the list definition.
print '<li class="server"><span class="server-name">' . $name . '</span>';
// Ping the server, and print an up or down status. You can use CSS to hide
// the 'Up' or 'Down' text and replace it with a graphic like a red or green
// status indicator.
$ping = new Ping($address);
$latency = $ping->ping();
if ($latency) {
print '<span class="status up">Up</span>';
}
else {
print '<span class="status down">Down</span>';
}
// End the list item definition.
print '</li>';
}
// End the unordered list.
print '</ul>';
?>
@geerlingguy
Copy link
Author

Sorry about that; it’s this PHP ping library: https://github.com/geerlingguy/Ping

@Netguykb
Copy link

Netguykb commented Aug 11, 2020 via email

@Mutale85
Copy link

Been using curl to check for server uptime, then was getting 0 code. I found this code useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment