通过popen获得命令的输出结果
int TransInterface::ping_process()
{
FILE *ptr;
char buff[512];
char ps[128];
char *pos;
memset(buff, 0, sizeof(buff));
memset(ps, 0, sizeof(ps));
sprintf(ps,"ping -c 5 -q %s|grep transmitted",m_workstationip.c_str());
if((ptr=popen(ps, "r")) != NULL)
{
fgets(buff, 512, ptr);
pos = strstr(buff, "100% packet loss");
if(pos == NULL) //ping success
{
m_conn_pingsuccess++;
DPRINT("m_conn_pingsuccess is %d\n",m_conn_pingsuccess);
if(m_conn_pingsuccess == 10){
DPRINT("system reboot,ping %s\n",buff);
system("reboot");
}
}else{
m_conn_pingsuccess = 0;
}
}
pclose(ptr);
return 0;
}