Page 1 of 1

Capture last 3 lines from ping output

Posted: Sat May 23, 2009 9:53 am
by alexd
Hi Guys,

I am building a network utility for my company to record the output of ping and traceroute results and put them in a database. Our manager will log into an admin section and run a report to see the summary of the ping and traceroute results.

I am able to store the output of the ping command in a variable. The output looks like this

My Account

Logged as alex.dehaini@teledataict.com | Settings | PingTest | SpeedTest | Logout
Ping Output:
PING 41.211.5.178 (41.211.5.178) 56(84) bytes of data.
64 bytes from 41.211.5.178: icmp_seq=1 ttl=254 time=1.18 ms
64 bytes from 41.211.5.178: icmp_seq=2 ttl=254 time=1.35 ms
64 bytes from 41.211.5.178: icmp_seq=3 ttl=254 time=1.11 ms
64 bytes from 41.211.5.178: icmp_seq=4 ttl=254 time=1.09 ms

--- 41.211.5.178 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 1.096/1.189/1.359/0.108 ms
I store the entire output in a variable. Now. I want to store only the last 3 lines in a variable i.e.
--- 41.211.5.178 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 1.096/1.189/1.359/0.108 ms
I was wondering if there is a function or some regular expression I could use to capture only the last 3 lines.

Any help would be greatly appreciated

Re: Capture last 3 lines from ping output

Posted: Sat May 23, 2009 5:33 pm
by Mark Baker
if you use exec() to execute the ping function, you can get the entire output in an array; otherwise explode your string variable on "\n" to break it into an array of lines.

Re: Capture last 3 lines from ping output

Posted: Sun May 24, 2009 7:03 pm
by alexd
For some reason, exec() captures only the last line of the output. It will be good to get the e I want to write the variable into the database.

Re: Capture last 3 lines from ping output

Posted: Sun May 24, 2009 8:46 pm
by mikemike
You might be better using something like Perl for this kind of thing, it's very similar to PHP syntax so you should have no issues.

I've only ever had trouble when working with exec and it grabbing outputs.

Re: Capture last 3 lines from ping output

Posted: Mon May 25, 2009 3:23 am
by MeLight
Why not use `some command` command instead of exec()?

Code: Select all

 
$result = `ping -c 5 google.com`;
$arr = explode("\n", $result);
print_r($arr);
 
works for me. (I'm on Linux, not sure if it makes a difference)

Re: Capture last 3 lines from ping output

Posted: Mon May 25, 2009 4:52 am
by Mark Baker
alexd wrote:For some reason, exec() captures only the last line of the output.
You are reading the format for exec() correctly?

Code: Select all

 
$output = array();
$command = 'ping -c 5 google.com';
$result = exec($command,$output,$status);
 
echo $result;      //  last line only
print_r($output);  //  all lines
 

Re: Capture last 3 lines from ping output

Posted: Tue May 26, 2009 4:49 am
by alexd
Thanks Mark, thanks MeLight.

Like I said - for some reason exec() only captures the last line; the solution you guys sent worked. I did not have a problem with capturing the entire output. I used the shell_exec() function.

Code: Select all

$outputstring = shell_exec("ping -c5 yahoo.com");
echo "<pre>$outputstring</pre>";
My issue was how to capture the last 2-3 lines from $outputstring.

I hope I am making sense - thanks for all the help

Re: Capture last 3 lines from ping output

Posted: Tue May 26, 2009 5:03 am
by onion2k
explode() it on "\n", then use array_slice() to grab the last three entries.

Re: Capture last 3 lines from ping output

Posted: Tue May 26, 2009 7:21 am
by alexd
Thanks onion2k,

It worked like a charm. I figured out another solution though - here is the code

Code: Select all

<?php
$string2= exec("ping -c 5 $address",$results);
$count=count($results);
$line= $count - 2;
$string1= $results[$line];
$string3=$string1.", ".$string2;
 
echo "<p>";
echo "<b>";
echo $string3;
echo "</b>";
echo "<p>";
echo "<hr>";
?>
It becomes pretty easy since I can write $string3 to a database.

Thanks guys