Jan 282013
 

I’ve been doing some coding for my Raspberry Pi’s Wireless Network Configuration page, and was trying to find a good way to get just a single number out of the block that ifconfig outputs –
I wanted to get the number after RX packets: in this example.

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2383877 errors:0 dropped:8221 overruns:0 frame:0
TX packets:2492088 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:734455593 (700.4 MiB) TX bytes:3075500605 (2.8 GiB)

I ended up writing a oneliner to do it for me, and put it into a function so I could use it more.
$strWlan0 is the block of code above.

substr($strWlan0,strpos($strWlan0,"RX packets:")+11,strpos(substr($strWlan0,strpos($strWlan0,"RX packets:")+11)," "));

If anyone has a better way of doing it though, please add it in the comments.

Share