LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   php ping script (https://www.linuxquestions.org/questions/programming-9/php-ping-script-472605/)

spank 08-10-2006 06:08 AM

php ping script
 
I want to do a script that echoes 1 if a host responds to ping and 0 if the host doesn't.

Any ideea on how to do that ?

spank 08-10-2006 07:43 AM

hope i'm not talking by myself :)

Code:

<?
function ping($ip){

        $cmd = shell_exec("ping -c 1 $ip");
        $cmd = explode(",",$cmd);
        if (eregi("0", $cmd[1]))
        {
                $con = 0;
        }
        if (eregi("1", $cmd[1]))
        {
                $con = 1;
        }
        return $con;
}

echo ping("google.com");
?>


sjuk 10-31-2006 04:41 AM

Hi

Im trying to do something very similar. I wrote a script that returns the ping time if it gets a reply, and NULL if ping times out. My problem is that if i dont get a reply the script takes about 10 seconds to time out. Im making this script for a status page of a wireless lan with about 200 units so u see why this is a problem.

Any ideas??

Did u get the script to work??

Ill post the script when i get home from work if u're interested.

sic

jlinkels 10-31-2006 05:09 AM

I have done the same to see if my internet connections are up or not. The script is written in Bash.

The timeout problem can be solved by specifying the -w or -W ping option. Maybe not necessary in your application of detecting wireless clients, but usually I recommend to check more than one ping reply using the count option. If there are routers or Cisco managed bridges in between your hosts the first ping might fail.

jlinkels

sjuk 10-31-2006 07:53 AM

I have tried -w and -t with no luck.

The shell_exec command stil uses 10 seconds to timeout at each offline router. The funny thing is that it works fine on windows.

jlinkels 10-31-2006 09:31 AM

Weird.... I am using this code, and when I specify "-w 1" the script returns in exactly 1 second. When I don't use -w it takes 10 seconds.

Are you sure you use lower case "w"

I use php5-cli to run this script.

jlinkels

Code:

<?
function ping($ip){

  $cmd = shell_exec("ping -c 1 -w 1 $ip");
  $cmd = explode(",",$cmd);
  if (eregi("0", $cmd[1]))
  {
    $con = 0;
  }
  if (eregi("1", $cmd[1]))
  {
    $con = 1;
  }
  return $con;
}

echo ping("10.90.20.1");
?>


sjuk 10-31-2006 02:38 PM

I think it's cause I use debian. Ping in debian doesn't have -w.
I wouldn't know(i'm a :newbie: ) but someone told me so.
I think there is something called fping with more options.
Anyway here's the code:

Code:

function ping($ip){
        $cmd = shell_exec("$ time ping -c 1 -w 1 $ip");
        $ping_results = explode(" ",$cmd);
            for($i=0;$i<count($ping_results)+1;$i++){
                                if(substr($ping_results[$i], 0, 5)=='time=' || substr($ping_results[$i], 0, 5)=='time<'){
                                        return substr_replace(substr_replace($ping_results[$i],'',0,5),'',-2,2);
                }
            }
                return NULL;
    }

sic

jlinkels 10-31-2006 05:40 PM

I use debian as well.

Try:
Code:

linkels@jlinkels_pc:/tmp$ ping -h
You'll get this reply:
Code:

Usage: ping [-LRUbdfnqrvVaA] [-c count] [-i interval] [-w deadline]
            [-p pattern] [-s packetsize] [-t ttl] [-I interface or address]
            [-M mtu discovery hint] [-S sndbuf]
            [ -T timestamp option ] [ -Q tos ] [hop1 ...] destination

Besides, there is an error in you shell_exec:
Code:

"$ time ping -c 1 -w 1 $ip"
won't work. Look at my code snippet for the correct syntax.

jlinkels

sjuk 11-01-2006 02:03 AM

I tried that but the -w parameter just isn't there. Maybe it's the version or something. I got our admin to install fping on the server. He should be done later today. I'll let u know how it went.

The shell_exec command i used is just the last desperate try in an endless line of tries. And i think the one u're using was the first one.

But thanx for trying.

sic


All times are GMT -5. The time now is 03:13 PM.