By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,689 Members | 1,731 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,689 IT Pros & Developers. It's quick & easy.

System("Ping") always returns 0

P: n/a
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.

Feb 14 '06 #1
Share this Question
Share on Google+
21 Replies


P: n/a
Neel wrote:
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>

--
Ian Collins.
Feb 14 '06 #2

P: n/a
Neel wrote:
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
Feb 14 '06 #3

P: n/a
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

Feb 14 '06 #4

P: n/a

Neel wrote:
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'.

Feb 14 '06 #5

P: n/a
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.

--
TB @ SWEDEN
Feb 14 '06 #6

P: n/a
My Redhat linux doesn't understand "fping".

Please someone help.

Feb 14 '06 #7

P: n/a
Neel wrote:
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).

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Feb 14 '06 #8

P: n/a

Neel wrote:
My Redhat linux doesn't understand "fping".

Please someone help.


goto comp.os.linux.networking

Feb 14 '06 #9

P: n/a
Are you sure that _popen will work with Redhat linux? it gives me
compile error.

Feb 14 '06 #10

P: n/a
TB
Neel sade:
Are you sure that _popen will work with Redhat linux? it gives me
compile error.


Did you include the necessary header file?

Try

~$> man -s 3 popen

--
TB @ SWEDEN
Feb 14 '06 #11

P: n/a
Neel wrote:
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.

--
Ian Collins.
Feb 14 '06 #12

P: n/a
TB wrote:
Neel sade:
Are you sure that _popen will work with Redhat linux? it gives me
compile error.


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

Feb 14 '06 #13

P: n/a
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.

Feb 14 '06 #14

P: n/a
Neel wrote:

I have also posted this question on comp.unix.programmer.
Good, see you there.
Help me guys.

I would if you quote correctly....

--
Ian Collins.
Feb 14 '06 #15

P: n/a
Neel wrote:
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Feb 14 '06 #16

P: n/a
Default User wrote:
popen() != _popen().

This is why off-topic advice is a BAD IDEA.


Yeah. He might also try the return value of pclose().

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Feb 14 '06 #17

P: n/a
Jerry Coffin wrote:
Neel wrote:
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.

--
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)
Feb 14 '06 #18

P: n/a
Neel wrote:
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
Feb 14 '06 #19

P: n/a
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
Feb 15 '06 #20

P: n/a
Sjouke Burry wrote:

[ ... ]
Why not redirect the ping output to a file,then read the
file to get your info?(Dont forget to delete the file)


There's no portable way to do I/O redirection either.

IOW, as has been pointed out a number of times already, doing this
requires platform-specific code, which indicates that it should be
discussed where the particular platform(s) in question are topical.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Feb 15 '06 #21

P: n/a
Jerry Coffin wrote:
Sjouke Burry wrote:

[ ... ]
Why not redirect the ping output to a file,then read the
file to get your info?(Dont forget to delete the file)
There's no portable way to do I/O redirection either.


It even depends on the particular implementation of ping sometimes, as
different versions can have differing output. When I did a task like
this many moons ago, the ping we had on our HPUX systems was different
from the one I'd previously used on Solaris, so I had to alter the
parsing routine (I used popen(), but that's irrelevant to this group).
IOW, as has been pointed out a number of times already, doing this
requires platform-specific code, which indicates that it should be
discussed where the particular platform(s) in question are topical.


Absolutely.

Brian
Feb 15 '06 #22

This discussion thread is closed

Replies have been disabled for this discussion.