嵌入式Linux的C应用代码里实现PING,并输出avg时间


在C应用代码里实现PING,并输出avg时间

int testshell( void )
{
	FILE   *stream;
    int len,i;
    char   buf[1024],tmp[256];
    char *p;
    p = (char *)tmp;
    stWMIpAddr.ucIPAddr[0] = 192;
    stWMIpAddr.ucIPAddr[1] = 168;
    stWMIpAddr.ucIPAddr[2] = 100;
    stWMIpAddr.ucIPAddr[3] = 150;
   strFmt(p, "ping -c1 %d.%d.%d.%d |grep avg |awk '{print $4}' |awk -F'/' '{print $2}'", stWMIpAddr.ucIPAddr[0],
            stWMIpAddr.ucIPAddr[1], stWMIpAddr.ucIPAddr[2],
            stWMIpAddr.ucIPAddr[3]);
	memset( buf, '/0', sizeof(buf) ); //初始化buf,以免后面写如乱码到文件中
    //stream = popen("ping -c1 192.168.100.153  |grep avg |awk '{print $4}' |awk -F'/' '{print $2}'", "r" );
    stream = popen(tmp, "r" );
    DEBUG("ping:\r\n");
     for(i=0; i<128; i++)
            DEBUG("%c",tmp[i]);
	len = fread( buf, sizeof(char), sizeof(buf), stream); //将刚刚FILE* stream的数据流读取到buf中
    if(len > 0)
    {
        debug(len, buf, "shell testRCV: ");
    }
    else
    {
        debug(0, NULL, "ping fail\n");
    }
	pclose( stream );
	return 0;
}

相关