Last active
March 4, 2021 22:41
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>'; | |
?> |
Thank you for this information. I was wondering if you can post the ping.php file? Also, Do you know if there are restrictions in running this on GoDaddy Servers? In other words are there other classes that need to be registered, or firewall ports opened, file security preferences needing to be set??
Thank you kindly sir.
Sorry about that; it’s this PHP ping library: https://github.com/geerlingguy/Ping
Thanks Jeff.
I’m actually wondering if you seen anything out there written in PHP, that would give me the results for example if I ping an IP addresses using CMD Line or terminal.
My goal is to query a DB that has a list of IP’s. Create an array and then display the results for all IP’s indicating the following (maybe output as JSON)
IP address: 100.200.300.400 64 bytes from 100.200.300.400
IP address: 500.600.700.800 64 bytes from 500.600.700.800
IP address: 900.110.120.130 Request timeout
I would like the results as described above or here is another screenshot from the terminal:
<img width="478" alt="Screen Shot 2020-08-11 at 10 52 46 AM" src="https://user-images.githubusercontent.com/58951223/89914755-7702cf80-dbc3-11ea-8119-dc4c918e11de.png">
Again having the code parse out in JSON
[
{ "ip_address":"108.176.82.50", "results":"64 bytes from 108.176.82.50"},{ "ip_address":"66.152.115.225", "results":"64 bytes from 66.152.115.225"},{ "ip_address":"209.217.208.162", "results":"Request timeout"}…
]
Thank you for any help
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
what exactly is in the ping.php file?
I'm trying to get this to work but I'm fairly new to this.