LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-10-2006, 06:08 AM   #1
spank
Member
 
Registered: Aug 2003
Location: Romania
Distribution: Ubuntu 6.06
Posts: 278

Rep: Reputation: 30
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 ?
 
Old 08-10-2006, 07:43 AM   #2
spank
Member
 
Registered: Aug 2003
Location: Romania
Distribution: Ubuntu 6.06
Posts: 278

Original Poster
Rep: Reputation: 30
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");
?>
 
Old 10-31-2006, 04:41 AM   #3
sjuk
LQ Newbie
 
Registered: Oct 2006
Posts: 7

Rep: Reputation: 0
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
 
Old 10-31-2006, 05:09 AM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 10-31-2006, 07:53 AM   #5
sjuk
LQ Newbie
 
Registered: Oct 2006
Posts: 7

Rep: Reputation: 0
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.
 
Old 10-31-2006, 09:31 AM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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");
?>
 
Old 10-31-2006, 02:38 PM   #7
sjuk
LQ Newbie
 
Registered: Oct 2006
Posts: 7

Rep: Reputation: 0
I think it's cause I use debian. Ping in debian doesn't have -w.
I wouldn't know(i'm a ) 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
 
Old 10-31-2006, 05:40 PM   #8
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 11-01-2006, 02:03 AM   #9
sjuk
LQ Newbie
 
Registered: Oct 2006
Posts: 7

Rep: Reputation: 0
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Title: PHP/MySQL Classifieds Script AddAsset1.php Script Insertion LXer Syndicated Linux News 0 07-02-2006 06:21 PM
ping script mcrosby Linux - General 33 06-29-2006 01:00 PM
issues with mirc script accessing our php script dtra Linux - Software 2 07-02-2005 04:30 PM
ping script richiehawtin Programming 7 08-28-2004 02:39 AM
PHP -- How to execute a shell script from PHP using FTP functions?? zoonalex Programming 3 07-29-2004 11:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration