Linux C 调用 ping命令 检测网络状态

int ping_status(char *ip)
{
    int i, status;
    pid_t pid;
	printf(">>>>>>>>>>>>> ping_status\n ");
    // 不同则循环检测多次
    for (i = 0; i < 3; ++i)
    {
        // 新建一个进程来执行ping命令
        if ((pid = vfork()) < 0)
        {
            printf("vfork error");
            continue;
        } 

        if (pid == 0)
        {
            // 执行ping命令
            //if (execlp("ping", "ping", "-c 1", ip, (char*)0) < 0)
			//if ( execlp("ping", "ping","-c 1",svrip, (char*)0) < 0)
			if ( execlp("ping", "ping","-c","1",ip, (char*)0) < 0)
            {
                printf("execlp error\n");
                exit(1);
            }
        }

        waitpid(pid, &status, 0);

        // 相等说明正常
        if (status == 0)
            return 0;

      //  sleep(2);
    }

    return -1;
}

  

posted @ 2017-07-05 09:48  慢伴拍的二叉树  阅读(6281)  评论(0编辑  收藏  举报