tags:

views:

1297

answers:

5

I have to call ping from c++ code.I'd like to easily read the output for further utilizations.

I have come up with two solutions:

  • use a fork and a pipe, redirect ping output to the pipe and then parse it
  • find a library suited for the purpose to use a ping(ip_addresss) function directly

I'd like the latter but i didn't find anything that was clearly a standard solution.

How would you do it ?

+2  A: 

I would go with your first option. Linux is built around the concept of having small, specialized apps which do one thing really well, communicating with pipes. Your app shouldn't include a library to implement ping, since there is already a built-in command to do it, and it works very well!

e.James
+1  A: 

You could try to use this code.

schnaader
+1  A: 

Check out BusyBox's source for 'ping' - you can use the ping4 and ping6 functions. Just be mindful of the GPL.

Spawning 'ping' should work too - check out popen(2) for a simpler API that also runs a shell. If it's a problem, pipe + fork + exec should work.

orip
+1  A: 

From the educational point of view invoking an external binary is very inadvisable. Especially for a simple task such as sending an ICMP echo request, you should learn a bit of socket.

Nicola Bonelli
A: 

I've managed to do like this:

I use popen which basically creates a pipe, fork and exec Then, if I need, i can wait with pclose.

nios