如何在Windows上以编程方式执行ping测试? [英] How to perform a ping-test programmatically on Windows?
问题描述
我想在 Windows 上的C命令行程序中进行IP测试。
I'd like to make an IP-test in a C command line program on Windows .
现在我在以下位置使用cmd命令我的程序是这样的:
Now I'm using the cmd-command in my program with something like this:
if(system("ping -c1 8.8.8.8 -w 2 ") == 0){
printf("request successful\n");
return true;
}else{
printf("request not successful\n");
return false;
}
请注意,上面的代码只是一个示例:在我的程序中,我将尝试ping设备,看它们是否在线;如果不是,我知道存在连接问题。因为我只需要连接状态,所以不需要显示结果。
Please note that the code above is just an example: with my program I will try to ping some devices, to see if they are online; if not I know there is a connection issue. Since I need only the connection status there is no need to show up the results.
是否有另一种方法可以通过编程方式进行同样的操作,因此无需使用cmd-window ?就像在后台隐藏请求一样。
Is there another way to do the same programmatically, so without cmd-window? Just like a hidden request in the background.
推荐答案
如果在 Windows 上,您需要以编程方式检查是否主机可以访问,我建议使用 _popen()
而不是 system()
。
If on Windows you need to check programmatically if an host is reachable, I suggest using _popen()
instead of system()
.
实际上,使用 管道 ,您可以使用 system()$ c $执行命令c>,但除此之外,其输出还会重定向到流。之后,您可以完全访问流,就像处理文件一样,读取输出并解析所需的内容。
In fact with pipes you can execute a command like with system()
, but in addition its output is redirected to a stream. After that you can access the stream exactly how you'd do with a file, reading the output and parsing whatever you need.
在此链接,您可以找到<$ c的Microsoft官方文档$ c> _popen()。您将能够轻松找到所有相关功能(例如 _pclose()
)。
At this link you can find Microsoft official documentation for _popen()
. You will be easily able to find all related functions (such as _pclose()
).
在以下示例程序中,<$发送c $ c> ping 命令(仅向Google DNS服务器请求两次回显以节省时间)。然后,以文本读取模式打开的获取的 FILE *
用于通过 normal 循环的 fread访问流。 ()
调用:
In the following demonstrative program a ping
command is sent (asking for only two echoes to Google DNS server in order to save time). Then the obtained FILE *
, opened in textual read mode, is used to access the stream with a normal loop of fread()
calls:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BUFFER_SIZE 1024
char buffer[BUFFER_SIZE] = { 0 };
int main( void )
{
FILE * pipe = _popen( "ping 8.8.8.8 -n 2", "rt" );
if( pipe != NULL )
{
int rd = 0, ret;
while( ( ret = fread( buffer+rd, 1, BUFFER_SIZE - rd, pipe ) ) > 0 )
{
rd += ret;
}
if( strstr( buffer, "TTL=" ) != NULL )
{
printf( "\nThe host is reachable!\n" );
}
else
{
printf( "\nThe host is NOT reachable!\n" );
}
//printf( "%d bytes read\n\n%s\n", rd, buffer );
_pclose( pipe );
}
else
{
printf( "Error in pipe opening!\n" );
}
return 0;
}
一些进一步的解释
- 在此示例中,仅验证简单主机 reachability 。如果至少有回声返回,则认为主机是可访问的。这是您可能需要解析的任何其他信息的起点。
- 我已经通过检查
TTL =
子字符串,如果可以成功ping操作,我肯定会以每种语言显示(根据PC设置,输出可能以不同的语言打印)。 - 将缓冲区大小调整为查找所需的子字符串所需的长度。在我的示例中,1024字节足以满足预期的响应长度。
- 您可以找到并注释整个缓冲区的打印内容。您可以使用该字符串检查所需的所有内容(例如平均ping时间)。
- 要从信息流中读取内容,请随时使用自己喜欢的内容功能。另一个流行的替代方法是
fgets()
,这一次可以很好地读取和解析一行,并且还需要较小的读取缓冲区。 - In this example only simple host reachability is verified. An host is considered reachable if at least an echo comes back. It is a starting point for any other information you might need to parse.
- I've done it by checking the presence of
TTL=
substring, that I'm sure will be present in every language in case of successful ping (the output may be printed in different languages according to PC settings). - Tailor your buffer size to the length you expect is required to find the needed substring. In my example 1024 bytes were far enough for the expected response length.
- You can find, commented, the print of the whole buffer. You can use that string to check everything you need (for example average ping time).
- In order to read from the stream, feel free to use your favourite function. Another popular alternative would be
fgets()
, that would be great to read and parse one line at a time, and would also require a smaller reading buffer.
Some further explanation
尽管问题是关于Windows的,但我不得不说基于 popen()
, pclose()
等,在Linux上将非常相似。
Though the question is about Windows, I have to say that the implementation on Linux would be very similar, based on popen()
, pclose()
and so on.
您可以在手册页。
这篇关于如何在Windows上以编程方式执行ping测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!