Jump to content

Trying to make an extremely simple server pinger


tgp1994

Recommended Posts

Hi there everyone, I'm trying to make a server pinger that pings my Garry's Mod game server to see if it's up. This shouldn't be very difficult, but it is for me since I'm new. As an added bonus, does anyone know if I can have it put text into a picture, with player info and such?

first: a pinger. simple, but I use extra PHP PEAR libraries to do it (faster, and bypasses running commands on the local server).  I can give you the basics of it, but if you can't install/don't have PEAR and Net_ping, my scripts won't do you much good.

 

second: you'll need to use PHP-GD to modify graphics.

Wow, thanks for the fast replies, guys. You should all hang out in the IRC room.

 

Anyways, does anyone have any examples, preferably small ones? If the GD image library is too much, then I won't bother.

 

And does anyone know why I have to do this image verification everytime I make a topic or reply?

Or if you prefer you can just use the system ping command using EXEC in your code.

 

http://us3.php.net/manual/en/function.exec.php

 

You can run any server command in your script via EXEC

if your host allows the web user to exec or shell_exec scripts.

 

As for the image verification, I'm guessing because you're new here.

echo "<pre>";

  system ("ping -c4 $host");

  system("killall ping");// kill all ping processes in case there are some stalled ones or use echo 'ping' to execute ping without shell

echo "</pre>";

 

the 4 is how many ping requests to send and $host is the IP address/domain  you wish to ping.

I've actually run this exact code on 3 different servers and on two the output was generated line by line where as on the 3rd the output was generated all at once.

 

I'm honestly not sure of the difference other than possibly OS but everything else matched up exactly the same.

if this server is your own server, it should be a breeze getting PEAR installed, and then installing Net_ping

go to the PHP folder (for me, it's C:\PHP5)

you will see a go-pear.bat file. run it

you will be prompted with a few questions. defaults are the safe road.

Now, you can go to the command line, and go to pear folder (for me, it's C:\PHP5\PEAR)

type this in

pear install Net_ping

now, you're set up with Net::ping

here's my ping script:

 

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
// Number of times to Ping the host
$count = $_GET['count'];
// Host we are pinging
$url = $_GET['ip'];
// This is the Pear Net_Ping class call
require_once "Net/Ping.php";
$ping = Net_Ping::factory();
$ping->setArgs(array('count'=>1,'timeout'=>1));
// Set variables. I know I don't have to, but why not do things right?
$transmitted = 0;
$received = 0;
$loss = 0;
$text = "";

for ($i=0; $i< $count; $i++){
        //Send ping request
        $output = $ping->ping($url);
        // If the ping returns no errors, lets parse the results.
        if (isset($output->_transmitted)){
                // Raw data, if you want to see what it returned from the ping (                                                                                                                               full text)
                $raw = $output->_raw_data;
                // This is the transmitted count. if it's zero, that means it fa                                                                                                                               iled in transmission. We shouldn't get a zero, but this is to handle that instan                                                                                                                               ce
                $trans = (string)$output->_transmitted;
                // if it successfully transmitted, lets parse the result, and ad                                                                                                                               d it up.
                if ($trans == 1){
                        $transmitted++;
                        $received += (string)$output->_received;
                        if ((string)$output->_loss != 0){
                                $loss++;
                                $text .=".";
                        }

                        else{
                                $text .="#";
                        }
                }
                else{
                        $transmitted++;
                        $loss++;
                        $text .=".";
                }
        }
}
echo <<<EOF
<?xml version="1.0" encoding="utf-8" ?>
<PingResults Version='1.0'>
        <PingResult Host='$url' Text='$text' Trasmitted='$transmitted' Received=                                                                                                                               '$received' Loss='$loss' />
</PingResults>
EOF;
?>

it outputs XML. you can change that to suit your needs.

Thank you, but the PEAR installation will most likely different for me since:

 

1.PHP installation failed when I was using IIS for unknown reasons, so:

2.I got WAMP, works awesomely

 

Thus, I think somehow I will mess up with the installation of PEAR, unless it comes with WAMP.

 

EDIT: Ok, I installed pear, and I navigate to the folder you suggested, (except a little different since I use WAMP), then it says: 'pear' is not a recognized internal or external command. And there doesn't seem to be a pear.exe file in there, anyways.

Lol I'm not sure if this is needed, but I think I may have to restart Apache. Otherwise, would you happen to know why it's saying:

 

Fatal error: require_once() [function.require]: Failed opening required 'Net/Ping.php' (include_path='.;C:\php5\pear') in C:\wamp\www\serverchecker\index.php on line 9

Weird, still gives me an error. Is there something I need to manually change in the php.ini file?

 

EDIT: Ok, I uncommented the include_path option and put in the PEAR directory, but now the error reads:

 

Notice: Undefined index: count in C:\wamp\www\serverchecker\index.php on line 5

Notice: Undefined index: ip in C:\wamp\www\serverchecker\index.php on line 7

find the line in the php.ini that says

;include_path=".;c:\php\includes"

and change it to

include_path = ".;c:\\WAMP\\php\\includes;c:\\WAMP\\php\\PEAR"

notice that I removed the semi-colon at the beginning, and added pear to the list. change the paths to fit your install

Ok, but there actually isn't an includes directory in my php installation. Is that a problem?

 

EDIT: And here's what I think the line should look like now:

 

include_path = ".;c:\\WAMP\\bin\\php\\php5.2.9-2\\includes;c:\\WAMP\\bin\php\\php5.2.9-2\\PEAR"

 

Does it seem right? Cause I still got the same two errors. Remeber, I still don't have an includes directory.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.