Capture last 3 lines from ping output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alexd
Forum Newbie
Posts: 7
Joined: Sat May 23, 2009 8:22 am

Capture last 3 lines from ping output

Post 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
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Capture last 3 lines from ping output

Post 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.
alexd
Forum Newbie
Posts: 7
Joined: Sat May 23, 2009 8:22 am

Re: Capture last 3 lines from ping output

Post 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.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Capture last 3 lines from ping output

Post 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.
MeLight
Forum Commoner
Posts: 26
Joined: Sun Apr 19, 2009 12:39 pm
Location: Israel

Re: Capture last 3 lines from ping output

Post 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)
Last edited by Benjamin on Tue May 26, 2009 11:01 am, edited 1 time in total.
Reason: Changed code type from text to php.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Capture last 3 lines from ping output

Post 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
 
alexd
Forum Newbie
Posts: 7
Joined: Sat May 23, 2009 8:22 am

Re: Capture last 3 lines from ping output

Post 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
Last edited by Benjamin on Tue May 26, 2009 11:01 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Capture last 3 lines from ping output

Post by onion2k »

explode() it on "\n", then use array_slice() to grab the last three entries.
alexd
Forum Newbie
Posts: 7
Joined: Sat May 23, 2009 8:22 am

Re: Capture last 3 lines from ping output

Post 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
Last edited by Benjamin on Tue May 26, 2009 11:02 am, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply