Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Manipulating 'ping' results

Manipulating 'ping' results
Thread Tools
Mac Enthusiast
Join Date: Nov 2002
Location: over yonder
Status: Offline
Reply With Quote
Aug 1, 2003, 01:35 AM
 
I'm looking for a way to manipulate the results of a *nix ping query using PHP.

I'd like to be able to store the ping times in an array that I can manipulate for a script. The only examples I can find all just export the entire result to the screen, while I would like to just have them entered into an array.

Would anyone be able to help with a code snippet?

Thanks a ton.
chown -R us:us yourbase

Dissent is not un-American.
     
Dedicated MacNNer
Join Date: Aug 2002
Status: Offline
Reply With Quote
Aug 1, 2003, 09:40 PM
 
lets see...

[php]
<?
$count=$HTTP_GET_VARS['count'];
$host=$HTTP_GET_VARS['host'];
$dumpedping=`ping -c $count $host`;
$pingarray=explode('\n',$dumpedping);
// so ping array now has each line... to find the time:
$index=0;
foreach($pingarray as $pingline) {
if($index>0) { // no data on first line
$time[$index]=ereg_replace(" .*","",ereg_replace(".*time=","",$pingline));
}
$index++;
}
// now time is an array with each time in it...
foreach($time as $data) {
echo("time: ".$data);
}
?>
[/php]


does that do what you need?

--will
     
Mac Enthusiast
Join Date: Nov 2002
Location: over yonder
Status: Offline
Reply With Quote
Aug 2, 2003, 01:11 AM
 
Yes, thanks...I didn't know you could dump the results of a system command into a variable. That makes it much easier!
chown -R us:us yourbase

Dissent is not un-American.
     
Dedicated MacNNer
Join Date: Aug 2002
Status: Offline
Reply With Quote
Aug 2, 2003, 10:37 AM
 
enjoy the wonders of the backtick operator

--will
     
Mac Enthusiast
Join Date: Nov 2002
Location: over yonder
Status: Offline
Reply With Quote
Aug 7, 2003, 03:19 PM
 
Originally posted by clam2000:
lets see...

[php]
<?
$count=$HTTP_GET_VARS['count'];
$host=$HTTP_GET_VARS['host'];
$dumpedping=`ping -c $count $host`;
$pingarray=explode('\n',$dumpedping);
// so ping array now has each line... to find the time:
$index=0;
foreach($pingarray as $pingline) {
if($index>0) { // no data on first line
$time[$index]=ereg_replace(" .*","",ereg_replace(".*time=","",$pingline));
}
$index++;
}
// now time is an array with each time in it...
foreach($time as $data) {
echo("time: ".$data);
}
?>
[/php]


does that do what you need?

--will
Thanks for the suggestions, but the code as written above does not work for several reasons. The backtick operator only returns the last line of the output, and the foreach loop with the ereg_replace functions produces an error.

I rewrote it and it now works as well as being slighly more compact.

[php]
<?
$count =$HTTP_GET_VARS ['count'];
$host =$HTTP_GET_VARS ['host'];
exec("ping -c $count $host", $dumpedping);

foreach($dumpedping as $pingline) {
ereg("time=[0-9]+[.]{1}[0-9]+", $pingline, $time);
$pingTime[] = $time[0];
$time[0] = NULL;
}

for($i = 0; $i < count($pingTime); $i++) {
$value = $pingTime[$i];
echo "$value<br />";
}
?>
[/php]
chown -R us:us yourbase

Dissent is not un-American.
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:15 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2