473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

System("Ping") always returns 0

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
21 30451
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
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
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

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
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
My Redhat linux doesn't understand "fping".

Please someone help.

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

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

Please someone help.


goto comp.os.linux.networking

Feb 14 '06 #9
Are you sure that _popen will work with Redhat linux? it gives me
compile error.

Feb 14 '06 #10
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sergio Geralnik | last post by:
I am trying to use this method to get a MethodInfo object but the method always returns a null. See my sample code below: this.button1 = new System.Windows.Forms.Button(); this.button1.Click +=...
8
by: Brian | last post by:
This is causing me to not be able to create a relation in my dataset. Here's my code: dc1 = ds.Tables .Columns ; dc2 = ds.Tables .Columns ; dr1 = new System.Data.DataRelation...
18
by: jrhoads23 | last post by:
Hello, I am trying to find a way to tell if an .NET windows forms Button (System.Windows.Forms.Button) is "depressed" (pushed down). For my application, I can not use a check box control set to...
6
by: Michi Henning | last post by:
I'm running the following code in Safari 2.0.4: document.cookie = 'MyCookie=1'; if(document.cookie == '') alert('document.cookie is empty!'); document.cookie always returns the empty string,...
5
by: Ofer Zelig | last post by:
Hello, I have a simple AJAX-enabled (RC 1.0) web site. Runs perfectly on my local machine (debugged by VS IDE). The problem is that on remote ("prodction") machine, every "satellite" file - JS,...
0
by: Anjan | last post by:
Hi All, I am new to Membership feature in ASP.NET 2.0. I am trying the following scenario. Environment : WinXP SP2 Framework: .NET v2.0, VS.NET 2005. User Data Storage : ADAM. I have an Web...
1
by: Bill Fisher | last post by:
Disclaimer, I am very, VERY new to .net. I am currently working with vb.net Visual Studio 2003.and I have a problem with the CurrentRowIndex property of the DataGrid control. I load an xml...
5
by: yogeshmk | last post by:
I'm writing an application which is required to function with many languages and also compile on Linux & windows. Here's how I find the locale .. # include <stdio.h> # include <locale.h> int...
2
by: brodyvon | last post by:
so the program wants me to output the a word based on what the punctuation the user used to end the sentence. This is what i have so far, i know its really bad... public class punctuation { ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.