System("Ping") always returns 0

N

Neel

I am trying to "ping" a remote host in my C++/Redhat Linux code to
check whether that host is connected or not.

if (0 == system("ping -w 2 192.168.0.2))

But, in both cases (connected/disconnected), system call returns 0. Can
someone please show what I am doing wrong? How to check the actual
result of the ping status using C++ on Redhat linux ?

Thanks in advance.
 
I

Ian Collins

Neel said:
I am trying to "ping" a remote host in my C++/Redhat Linux code to
check whether that host is connected or not.

if (0 == system("ping -w 2 192.168.0.2))

But, in both cases (connected/disconnected), system call returns 0. Can
someone please show what I am doing wrong? How to check the actual
result of the ping status using C++ on Redhat linux ?
Repost to comp.unix.programmer.

<OT>system returns the result of forking the command, not the command
return<OT>
 
L

Larry I Smith

Neel said:
I am trying to "ping" a remote host in my C++/Redhat Linux code to
check whether that host is connected or not.

if (0 == system("ping -w 2 192.168.0.2))

But, in both cases (connected/disconnected), system call returns 0. Can
someone please show what I am doing wrong? How to check the actual
result of the ping status using C++ on Redhat linux ?

Thanks in advance.

Here's a snip from 'man system':

[snip]
RETURN VALUE
The value returned is -1 on error (e.g. fork failed), and the return
status of the command otherwise. This latter return status is in the
format specified in wait(2). Thus, the exit code of the command will
be WEXITSTATUS(status). In case /bin/sh could not be executed,
the exit status will be that of a command that does exit(127).
[snip]

So,

#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int ping_ret, status;

status = system("ping -w 2 192.168.0.2");

if (-1 != status)
ping_ret = WEXITSTATUS(status);

Regards,
Larry
 
N

Neel

Thanks a lot Larry for help.

BUT,
as I said, I would like to know the ping result (If I get a ping reply
from remote host, its TRUE) and (if I don't get a ping reply from
remote host, its FALSE).

I used WEXITSTATUS(status) with remote host disconnected. I am still
getting a TRUE. Please see below my code.

sprintf(pingstring,"ping -w 2 %s",ip_add);

// Execute 'ping' command via the system call
status = system(pingstring); //lint !e421 function 'system(const
char *)' is considered dangerous [MISRA Rule 126]

PDEBUG("Ping returned status:%d", status);

if (-1 != status)
{
ping_ret = WEXITSTATUS(status);

PDEBUG("Ping WEXITSTATUS ping_ret:%d", ping_ret);

if(0 == ping_ret)
{
result = TRUE;
}
}

In both cases when remote host replys or doesn't reply, i get
"ping_ret" as 0.

Let me know what i am doing wrong here.
Thanks for this stupid question
 
R

roberts.noah

Neel said:
I am trying to "ping" a remote host in my C++/Redhat Linux code to
check whether that host is connected or not.

if (0 == system("ping -w 2 192.168.0.2))

But, in both cases (connected/disconnected), system call returns 0. Can
someone please show what I am doing wrong? How to check the actual
result of the ping status using C++ on Redhat linux ?

Thanks in advance.

Check man page of ping and make sure it does what you want.

I have a feeling you need a different command, such as 'fping'.
 
T

TB

Neel sade:
Thanks a lot Larry for help.

BUT,
as I said, I would like to know the ping result (If I get a ping reply
from remote host, its TRUE) and (if I don't get a ping reply from
remote host, its FALSE).

I used WEXITSTATUS(status) with remote host disconnected. I am still
getting a TRUE. Please see below my code.

sprintf(pingstring,"ping -w 2 %s",ip_add);

// Execute 'ping' command via the system call
status = system(pingstring); //lint !e421 function 'system(const
char *)' is considered dangerous [MISRA Rule 126]

PDEBUG("Ping returned status:%d", status);

if (-1 != status)
{
ping_ret = WEXITSTATUS(status);

PDEBUG("Ping WEXITSTATUS ping_ret:%d", ping_ret);

if(0 == ping_ret)
{
result = TRUE;
}
}

In both cases when remote host replys or doesn't reply, i get
"ping_ret" as 0.

Let me know what i am doing wrong here.
Thanks for this stupid question

If 'ping' is a system program, then you need to parse the output
if you want to find out if the remote host was reachable or not.
Pipe and regex it. The return value only states if 'ping' executed
successfully or not.
 
P

Phlip

Neel said:
I am trying to "ping" a remote host in my C++/Redhat Linux code to
check whether that host is connected or not.

if (0 == system("ping -w 2 192.168.0.2))

You will ultimately be much better off opening a socket on the computer and
port you want. Pings are for interactive command lines. If you open a socket
you will get any error message Ping would have returned (as an int, not a
string), and you will further verify the port you want is available.
But, in both cases (connected/disconnected), system call returns 0.

Use _popen("ping -c 4 127.0.0.1", "r"), and collect the string output of the
ping. And remember the -c 4, so your Linux pinger doesn't ping forever
(which is its incomprehensibly stupid default).
 
I

Ian Collins

Neel said:
Thanks a lot Larry for help.

BUT,
as I said, I would like to know the ping result (If I get a ping reply
from remote host, its TRUE) and (if I don't get a ping reply from
remote host, its FALSE).
You should
a) quote what you are replying to.
b) go where I directed you, if you had quoted, I could have set the
appropriate follow-up.

The example Larry posted:

#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int ping_ret, status;

status = system("ping -w 2 192.168.0.2");

if (-1 != status)
ping_ret = WEXITSTATUS(status);

works (at least on my system), read you man pages for the return values
of ping and sent any question to comp.os.linux.networking or
comp.unix.programmer.
 
D

Default User

TB said:
Neel sade:

Did you include the necessary header file?

Try

~$> man -s 3 popen

popen() != _popen().


This is why off-topic advice is a BAD IDEA. The OP needs to go to a
newsgroup dedicated to his platform, where he can get accurate advice
for his situation.



Brian
 
N

Neel

1)
I did the same thing what you told me to do.

#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>


int ping_ret, status;


status = system("ping -w 2 192.168.0.2");


if (-1 != status)
ping_ret = WEXITSTATUS(status);


But, even if ethernet cable is connected or not to the remote host, I
always get "Ping_ret" = 0.

2)
I also tried popen:

char psBuffer[1024];
FILE * fdRTP;


if( (fdRTP = popen( "ping -c 1 192.168.0.2", "r" )) == NULL )
{
FSLog(LOG_ERROR, "AebEthernetTest::HandleDmseCommand(): Failed to
create Pipe");
error_code = AEB_DPI_INIFILEREADERR;
}

else
{
/* Read this pipe and print to the stdout until end of file */
while( !feof( fdRTP ) )
{
if( fread( psBuffer, sizeof(char), 1024, fdRTP ) )
{
if(0 == strcmp(psBuffer,"0% loss"))
{
result = TRUE;
}
}
}
}

/* Close pipe */
pclose( fdRTP );

But, in this case, its not even "Ping".

I have also posted this question on comp.unix.programmer.

Help me guys.
 
J

Jerry Coffin

Neel said:
I am trying to "ping" a remote host in my C++/Redhat Linux code to
check whether that host is connected or not.

if (0 == system("ping -w 2 192.168.0.2))

But, in both cases (connected/disconnected), system call returns 0. Can
someone please show what I am doing wrong? How to check the actual
result of the ping status using C++ on Redhat linux ?

What you're doing wrong is expecting the return value from system to
mean something. Except when you pass a null pointer, the return value
from system is implementation defined -- you can't (portably) depend on
it meaning anything.

In my own experience, it usually reflects one of two things -- it can
reflect the return value from whatever you asked it to do. It can also,
however, reflect only whether your invocation of the command processor
itself succeeded -- i.e. whether what you specified was a valid command
that could be executed to at least some degree (e.g. that it could find
an executable named ping somewhere on the path -- but nothing more).

I'm reasonably certain there is no portable way to do what you want.
Almost every system I know of has some way to do it, but TTBOMK, it's
never been standardized.
 
S

Sjouke Burry

Jerry said:
What you're doing wrong is expecting the return value from system to
mean something. Except when you pass a null pointer, the return value
from system is implementation defined -- you can't (portably) depend on
it meaning anything.

In my own experience, it usually reflects one of two things -- it can
reflect the return value from whatever you asked it to do. It can also,
however, reflect only whether your invocation of the command processor
itself succeeded -- i.e. whether what you specified was a valid command
that could be executed to at least some degree (e.g. that it could find
an executable named ping somewhere on the path -- but nothing more).

I'm reasonably certain there is no portable way to do what you want.
Almost every system I know of has some way to do it, but TTBOMK, it's
never been standardized.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Why not redirect the ping output to a file,then read the
file to get your info?(Dont forget to delete the file)
 
L

Larry I Smith

Neel said:
Thanks a lot Larry for help.

BUT,
as I said, I would like to know the ping result (If I get a ping reply
from remote host, its TRUE) and (if I don't get a ping reply from
remote host, its FALSE).

I used WEXITSTATUS(status) with remote host disconnected. I am still
getting a TRUE. Please see below my code.

sprintf(pingstring,"ping -w 2 %s",ip_add);

// Execute 'ping' command via the system call
status = system(pingstring); //lint !e421 function 'system(const
char *)' is considered dangerous [MISRA Rule 126]

PDEBUG("Ping returned status:%d", status);

if (-1 != status)
{
ping_ret = WEXITSTATUS(status);

PDEBUG("Ping WEXITSTATUS ping_ret:%d", ping_ret);

if(0 == ping_ret)
{
result = TRUE;
}
}

In both cases when remote host replys or doesn't reply, i get
"ping_ret" as 0.

Let me know what i am doing wrong here.
Thanks for this stupid question

Ok, so do 'man system', and read the paragraph on 'Return Value'.
Your version of system() may be different (is that allowed?).

Larry
 
D

Default User

Larry I Smith wrote:

Ok, so do 'man system', and read the paragraph on 'Return Value'.
Your version of system() may be different (is that allowed?).


Yes. Here's what it says in the C standard.

[#3] If the argument is a null pointer, the system function
returns nonzero only if a command processor is available.
If the argument is not a null pointer, and the system
function does return, it returns an implementation-defined
value.


So anything it does return in the example given is going to be specific
to the implementation. So, as many have said, the OP needs to move to a
newsgroup dedicated to his platform.



Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top