最近在写一个定时监控公司服务器和其他网络设备运行状态的小程序。
下面代码是 网上收集 自己修改的
QString JCode::PingIP(QString IP) //Ping IP地址 { bool m_state=false; QString str="失败"; QProcess cmd; //类似于window里面的cmd命令 QTextCodec *codec = QTextCodec::codecForName("GBK"); QString outstr; cmd.start("ping "+IP + " -n 1"); //ping ip地址 -n 1表示只ping一次就结束 cmd.waitForFinished(-1); //等待ping完成 outstr = codec->toUnicode(cmd.readAll()); //将ping之后的结果全部读到outstr中 if(-1!=outstr.indexOf("往返行程的估计时间")) //通过判断有无“往返行程的估计时间”这几个字来检测网路是否通 { if(!m_state) //通过m_state变量使网络无论在通或不通只发送一次信号 { str="成功"; } } else { if(m_state) { str="失败"; } } return str; } QString JCode::SYSINFO(){ //获取操作系统信息 QString str=""; str+=QString("WindowsVersion:%1\n").arg(QSysInfo::WindowsVersion); str+=QString("buildAbi:%1\n").arg(QSysInfo::buildAbi()); str+=QString("buildCpuArchitecture:%1\n").arg(QSysInfo::buildCpuArchitecture()); str+=QString("currentCpuArchitecture:%1\n").arg(QSysInfo::currentCpuArchitecture()); str+=QString("kernelType:%1\n").arg(QSysInfo::kernelType()); str+=QString("kernelVersion:%1\n").arg(QSysInfo::kernelVersion()); str+=QString("machineHostName:%1\n").arg(QSysInfo::machineHostName()); str+=QString("prettyProductName:%1\n").arg(QSysInfo::prettyProductName()); str+=QString("productType:%1\n").arg(QSysInfo::productType()); str+=QString("productVersion:%1\n").arg(QSysInfo::productVersion()); str+=QString("Windows Version:%1\n").arg(QSysInfo::windowsVersion()); return str; } QString JCode::GetIPadd(){ //获取IP地址 QString localHostName = QHostInfo::localHostName(); QString tmp=""; tmp+= QString("主机名:%1\n").arg(localHostName); QHostInfo info = QHostInfo::fromName(localHostName); foreach(QHostAddress address,info.addresses()) { if(address.protocol() == QAbstractSocket::IPv4Protocol) tmp+= QString("IP地址:%1\n").arg(address.toString()); } return tmp; } QString JCode::GetMACadd(){ QString address; auto interfaces = QNetworkInterface::allInterfaces(); for (int i = 0; i < interfaces.length(); i++) { if (interfaces[i].isValid()) { address = interfaces[i].hardwareAddress(); break; } } if (address.isEmpty()) { address="ff:"; } return address; }
发表评论