7

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.

vitaut
  • 49,672
  • 25
  • 199
  • 336
Samuel
  • 574
  • 3
  • 7
  • 22
  • Doesn't a Win32 application (with WinMain()) prevent the command line from appearing? – Mateen Ulhaq Oct 31 '10 at 06:44
  • 1
    I don't know about suppressing the output, but pinging is one of those situations where you generally avoid system(). –  Oct 31 '10 at 06:47
  • @muntoo: Calling a console program means it needs a console; console programs have a special flag in Windows' PE format, so they're slightly different. –  Oct 31 '10 at 06:48
  • I would love to know how to ping without using the system ping, but I am fairly new to programming and so what I have found online has been extremely confusing at best :( – Samuel Oct 31 '10 at 07:06

6 Answers6

21

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

uınbɐɥs
  • 7,236
  • 5
  • 26
  • 42
zvrba
  • 24,186
  • 3
  • 55
  • 65
  • 2
    If only I could mark you as an answer... but sadly I didn't ask the question. – Corey Ogburn Oct 31 '10 at 06:57
  • 4
    No need for the colon, "nul" is quite sufficient. – avakar Oct 31 '10 at 07:01
  • Thank you, this answer is very succinct and awesome. – Samuel Oct 31 '10 at 07:08
  • 3
    @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 Oct 31 '10 at 10:17
  • @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 Oct 31 '10 at 11:40
  • 2
    @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 Oct 31 '10 at 12:29
  • @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 Oct 31 '10 at 17:18
  • 2
    @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 Oct 31 '10 at 17:24
  • 1
    @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 Oct 31 '10 at 17:40
6

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 give you control over how the other program runs, for instance, that it not show output or not create a console window, etc.

Max Lybbert
  • 19,717
  • 4
  • 46
  • 69
3

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
  • 6,144
  • 9
  • 44
  • 66
  • Thank you bjskishore, it is very good to know how I can send the output to file if need be. – Samuel Oct 31 '10 at 07:09
2

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
  • 15,513
  • 2
  • 20
  • 26
  • Thank you for you answer, I would love more about hiding the console window, that would be awesome. – Samuel Oct 31 '10 at 07:08
2

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
  • 2,345
  • 2
  • 17
  • 24
  • What library is lpStartupInfo under? – Samuel Nov 02 '10 at 06:07
  • Hi Samuel. lpStartupInfo is the second-to-last parameter to the CreateProcess() function. Please consult the function's documentation for the details (see the link in my original post). – CoreTech Nov 04 '10 at 12:07
2

You could also use this way, this will return the output in a file and doesn't show up a console windows and freezes the main application which is really usefull. At first you need to include the Windows header using;

#include <Windows.h>

then send a ping command and write the output into a file like this;

WinExec("ping google.com > file.dat", SW_HIDE); 

This will send a ping command to google.com and writes the output to the file 'file.dat' in the directory of your current running program. So you could change file.dat to any file or filepath you want and of course you could change the ping command. The > character means that the output of the command needs to be wrote in the file path behind it. If you want to show the console window and freeze the application while running the ping command you need to use the following line of code instead of the WindExec() code;

system("ping google.com > file.dat");
Tim Visée
  • 2,988
  • 4
  • 45
  • 55