Linux C 怎么检测网络状态
Linux C 怎么检测网络状态#include#include#includeping_check (char *gateway_str){printf("ping_check ::gateway_str -- %s\n",gateway_str);FILE *fp;size_t len = 0;char *pos;int r
·
Linux C 怎么检测网络状态
#include <stdio.h>
#include<unistd.h>
#include<string.h>
ping_check (char *gateway_str)
{
printf("ping_check ::gateway_str -- %s\n",gateway_str);
FILE *fp;
size_t len = 0;
char *pos;
int ret,percent = 100;
char* line = NULL; char cmd[80]={0};
sprintf(cmd, "ping -c 1 -q -n %s >ping_gatway.txt",gateway_str);
system(cmd);
fp = fopen("ping_gatway.txt", "r");
if (!fp){
goto error1;
}
while (getline (&line, &len, fp) != -1) {
if (strncmp ("packets transmitted", line + 2, 19) != 0)
{
continue;
}
pos = strchr (line, '%');
if (pos == 0) {
goto error1;
}
while (*pos != ' ')
{
pos--;
}
sscanf (pos, " %d%% loss", &percent);
printf("ping_check->pos -- %s\n", pos, );
printf("ping_check->percent -- %d \n", percent);
if (percent == 0) {
printf("ping_check:OK\n");
ret = PING_TEST_IPV6_OK;
}else {
printf("ping_check: NGNG \n");
}
}
error1:
free(line);
ping_err_ipv6 = ret;
printf ("ping_check:ret =%d\n",ret);
return ret;
}
更多推荐
已为社区贡献1条内容
所有评论(0)