1. 功能代码
static bool sureConnect(const std::string &ip)
{
// ping命令,-c 4意味着发送4个包,-W设置命令超时时间,> /dev/null令输出不回显
std::string command = "ping -W 0.6 -c 1 " + ip + " > /dev/null";
// 执行ping命令
int returnCode = -1;
//对于无法联通设备进行多次确认,因为ping有概率丢包
for (int i = 0; i < 5; i++) {
returnCode = system(command.c_str());
if (returnCode == 0) {
return true;
}
FGLOG(ERROR) << "IP " << ip << " The " << i + 1 << " connection fails";
}
return false;
}
2. 功能介绍
使用system函数结合ping命令快速完成对某个地址是否联通的判断。因为ping本身可能会发生丢包,为了提高稳定性对无法连接设备做了多次检测。