views:

128

answers:

5

Hi guys,

I have written a simple program that pings three sites and then reacts to whether they are reachable or not.

My question is: can I suppress system("ping ")'s output? I have written my code in C++ as I know that language the best. Currently the code opens the ping.exe running the system command. If I can prevent the output from showing up while it still pings that would be ideal.

I am eventually going to turn this program in a windows service that is why I would like to suppress both the command line console window as well as suppress the ping output. Thanks.

+3  A: 

You can use system command like below to suppress the output of ping command.

system("ping 100.100.100.100 > response.dat");

Above command pings IP address 100.100.100.100 and directs the output to a file called response.dat. In response.dat you can see the response of ping command.

bjskishore123
Thank you bjskishore, it is very good to know how I can send the output to file if need be.
Samuel
+6  A: 

Try doing system("ping host > nul:") (nul: is windows equivalent of UNIX /dev/null).

zvrba
If only I could mark you as an answer... but sadly I didn't ask the question.
Corey Ogburn
No need for the colon, "nul" is quite sufficient.
avakar
Thank you, this answer is very succinct and awesome.
Samuel
@Samuel,@zvrba: I can put a malicious executable named `ping.exe` and your program would be screwed. You should almost never use `system()`: http://www.cplusplus.com/forum/articles/11153/ If you're making a Windows Service, then the situation is at least twice as bad.
Lie Ryan
@Lie: 1) I rarely second-guess the OP. He got the answer to his question. 2) If your system is compromised, you're screwed whatever method of spawning you use, and whichever process you spawn. So what exactly is your point?
zvrba
@zvrba: Even if his executable has an execute-only permission (i.e. you can't compromise the executable), but if the program's current directory is a directory that everyone has a write access to (which is quite often the case), then you don't need a compromised system to replace `ping.exe` with a malicious program.
Lie Ryan
@Lie: um, so an unknown executable is by some means (irrelevant which means) dropped into some directory and awaits to be executed. So how would you call that if not compromised system?
zvrba
@zvrba: This is a common scenario in any shared system. If any of your low-privilege users can write to that directory, they can gain your program's privileges; in other word, if you're using system() call, you're leaking your program's privilege to everyone on the system. Unless you can fully trust all your users (which is **never** the case in a shared systems, unless you're delusional), then your program using system() is a security hazard.
Lie Ryan
@zvrba: this particular argument about not using system() is moot if the program you're writing is meant to be executed by the user himself, since programs you execute yourself will use your own privilege (unless configured otherwise by the system administrator); but the OP is writing a Windows Service, which means the program is very likely to have a different privilege level than the current user, this is a real security consideration.
Lie Ryan
+2  A: 

Do system( "ping site.com >nul 2>nul" ); and check the value the shell returns. if the ping succeeds, the shell will return 0, else it will return 1. I would be more detailed, but Vis Studio is reinstalling itself. :)

There's also a way to hide the console window using the Win API to exec the command, but... I do not remember the details.

Edit: I'm still waiting for the MSVS install process, so... :) Use CreateProcess with the DETACHED_PROCESS flag for the dwCreationFlags parameter to hide the console window.

After you call create process, you'll have to use WaitForSingleObject on the process handle to wait for the ping to complete. The last parameter to CreateProcess should have a pointer to process information that contains the process handle. (Assuming CreateProcess was successful) You have to wait for the command to complete. Once it's complete, you can use the process handle to get the return value, though I'm too time contstrained to tell you how to do that at this point.

JimR
Thank you for you answer, I would love more about hiding the console window, that would be awesome.
Samuel
+4  A: 

Generally, if you're going to call another program but don't want it to act like std::system, you're going to need a platform-specific function like fork()/exec on UNIX or CreateProcess() on Windows. These functions allow you to pass arguments to not show output or not create a console window, etc.

Max Lybbert
+1  A: 

Hi Samuel.

When you get over to Windows and call CreateProcess(), be sure to set:

    lpStartupInfo->wShowWindow = SW_HIDE;

This will ensure that any windows created by the new process are hidden.

Using the DETACHED_PROCESS flag will prevent the new process from inheriting your application's console, but that does not prevent the new process from creating a new console. Not sure what ping would do, but best to remove all doubt by using SW_HIDE.

CoreTech