简体   繁体   中英

How to capture ping's return value in C++

Does anyone know, how to capture ping's return value in c++? According to this link : ping should return 0 on success, 1 on failure such as unknown host, illegal packet size, etc. and 2 On a unreachable host or network.

In C++ I called ping with the system () , eg int ret = system("ping 192.168.1.5"); .

My problem is, that ret 's value is 0 or 1 . It will never 2 ! If I think correctly, this is because, this return value I get, is the system functions return value, not ping's. So how could i get ping's return vlaue?

Thanks in advance!

kampi

Edit: Right now i use this system("ping 192.169.1.5 > ping_res.txt"); but i don't want to work with files (open, and read them), that's why i want to capture, th return value , if possible :)

如果您使用的是Windows,最好直接使用IcmpSendEcho2在应用程序中实现ping功能。

A simple solution would be pipe the output of ping to to a file and read it.

Eg

system("ping 192.169.1.5 > ping_res.txt");

And read ping_res.txt to get the info you need.

From man 3 system on Linux:

RETURN VALUE
The value returned is -1 on error (eg fork(2) failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2).

Then from man 2 wait on Linux:

If status is not NULL, wait() and waitpid() store status information in the int to which it points. This integer can be inspected with the following macros (which take the integer itself as an argument, not a pointer to it, as is done in wait() and wait- pid()!):

WIFEXITED(status)
returns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning from main().

WEXITSTATUS(status)
returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) or as the argument for a return statement in main(). This macro should only be employed if WIFEXITED returned true.

From sys/wait.h on Linux:

# define WEXITSTATUS(status)    __WEXITSTATUS(__WAIT_INT(status))

From bits/waitstatus.h on Linux:

/* If WIFEXITED(STATUS), the low-order 8 bits of the status.  */
#define __WEXITSTATUS(status)   (((status) & 0xff00) >> 8)

In other words, you will wan to use these macros if you are using Linux. Are you using HP-UX? I notice you link to information for HP-UX. If so, what does your man 3 system page say?

Also, keep in mind that system invokes "sh -c command " and you will receive the return value of sh:

EXIT STATUS
The following exit values shall be returned:
0 The script to be executed consisted solely of zero or more blank lines or comments, or both.
1-125 A non-interactive shell detected a syntax, redirection, or variable assignment error.
127 A specified command_file could not be found by a non-interactive shell.
Otherwise, the shell shall return the exit status of the last command it invoked or attempted to invoke (see also the exit utility in Special Built-In Utilities).

What return value do you find if you attempt, eg, system("exit 203"); ?

So you just want to know the return value: ie " 0 on success, 1 on failure such as unknown host, illegal packet size, etc. and 2 On a unreachable host or network. " I think using ping is an overkill in this case. Writing to file and reading from it is apparently a little bit ugly.

I think you should just try **open()**ing a connection to the host on port 7 (Echo). Then you would get the information that ping return value would tell you. If you further want to get response durations, you can measure it yourself by sending arbitrary data to the host on echo port. It would take some time to write the code but i think it's worth for flexibility and accuracy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM