加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

QProcess调用外部ping程序实现网络状态检测

(2017-05-09 21:57:49)
分类: linux
转自:QProcess调用外部ping程序实现网络状态检测

http://img.blog.csdn.net/20160923170520521?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

上代码:

 

  1.   
  2. #ifndef SHELLPROCESS_H  
  3. #define SHELLPROCESS_H  
  4.   
  5. #include   
  6. #include   
  7. #include   
  8.   
  9. //命令行工具类  
  10. class ShellProcess public QThread  
  11.  
  12.     Q_OBJECT  
  13. public 
  14.     explicit ShellProcess(QObject *parent 0);  
  15.     QStringList getIpRange();  
  16. public 
  17.     static bool isAlive(QString ip, int port, int timeout 1000);  
  18. signals:  
  19.     void commandSuccess(QString ip);  
  20.     void commandFailed(QString ip);  
  21. public slots:  
  22.     void setIpRange(QStringList ipRange);  
  23.     void readResult();  
  24.     void startedThread();  
  25. protected 
  26.     void run();  
  27. private 
  28.     void bindEvent();  
  29. private 
  30.     QStringList ipRange;  
  31.     QProcess* m_process;  
  32. };  
  33.   
  34. #endif // SHELLPROCESS_H  
  35.   
  36.   
  37.   
  38. #include "shellprocess.h"  
  39. #include   
  40. #include   
  41. #include   
  42.   
  43. #define USE_PARSE_RESULT //使用解析返回内容方式  
  44.   
  45. ShellProcess::ShellProcess(QObject *parent)  
  46.     QThread(parent)  
  47.  
  48.     m_process new QProcess(this);  
  49.     this->bindEvent();  
  50.  
  51.   
  52. void ShellProcess::bindEvent()  
  53.  
  54.     connect( thisSIGNAL(started()), thisSLOT(startedThread()) );  
  55.     connect( m_process, SIGNAL(readyReadStandardOutput()), thisSLOT(readResult()) );  
  56.  
  57.   
  58. QStringList ShellProcess::getIpRange()  
  59.  
  60.     return this->ipRange;  
  61.  
  62.   
  63. void ShellProcess::setIpRange(QStringList ipRange)  
  64.  
  65.     this->ipRange ipRange;  
  66.  
  67.   
  68. void ShellProcess::startedThread()  
  69.  
  70. #ifndef USE_PARSE_RESULT  
  71.     return  
  72. #endif  
  73.   
  74.     QString ip;  
  75.     foreach( ip, ipRange  
  76.      
  77. #ifdef Q_OS_WIN  
  78.         QString strArg "ping -n -i "ip;  
  79.         m_process->start(strArg);//异步,才可以看到输出  
  80.         //m_process->execute(strArg);//同步,直接打印了  
  81. #else  
  82.         QStringList argstrList;  
  83.         argstrList << "-c 1" << "-t 2" << ip;  
  84.         m_process->start("ping"argstrList);//异步,才可以看到输出  
  85.         //m_process->execute("ping", argstrList);//同步,直接打印了  
  86. #endif  
  87.      
  88.   
  89.  
  90.   
  91. void ShellProcess::run()  
  92.  
  93. #ifdef USE_PARSE_RESULT  
  94.     return ;//结束线程  
  95. #endif  
  96.   
  97.     QString ip;  
  98.     foreach( ip, ipRange  
  99.      
  100.         qDebug() << "ping " ip;  
  101.         //qDebug()<<"判断"<<ip<<"mysql在线与否:"<<isAlive(ip, 3306, 100);  
  102.         int exitCode 0;  
  103. #ifdef Q_OS_WIN  
  104.         //启动一个ping进程,然后等待该进程结束。  
  105.         QString strArg "ping " ip -n -i 2" 
  106.         exitCode QProcess::execute(strArg);  
  107. #else  
  108.         exitCode QProcess::execute("ping"QStringList() << "-c 1" << "-t 2" << ip);  
  109. #endif  
  110.         if == exitCode  
  111.             // it's alive  
  112.             qDebug() << "shell ping " ip success" 
  113.             emit commandSuccess(ip);  
  114.         else  
  115.             qDebug() << "shell ping " ip failed" 
  116.             emit commandFailed(ip);  
  117.          
  118.      
  119.   
  120.  
  121.   
  122. //解析Console打印内容  
  123. void ShellProcess::readResult()  
  124.  
  125.     QProcess *ping qobject_cast(sender());  
  126.     if !ping  
  127.         return 
  128.     QTextCodec *codec QTextCodec::codecForName("GBK");//指定QString的编码方式  
  129.     QString res codec->toUnicode( ping->readAllStandardOutput() );      
  130.     QString ip ping->arguments().last();  
  131. #ifdef Q_OS_WIN  
  132.     if !res.contains('%' 
  133.         return 
  134.     const int percent res.mid(res.indexOf('('), res.indexOf(')')).section('%'0, 0).remove('(').toInt();  
  135.     qDebug()<<res<<percent;  
  136.     if percent == 100  
  137.         qDebug() << ip << "host not found." 
  138.         emit commandFailed(ip);  
  139.     else  
  140.         qDebug() << ip << "host found." 
  141.         emit commandSuccess(ip);  
  142.      
  143. #else  
  144.     qDebug()<<res;  
  145. #endif  
  146.  
  147.   
  148. //判断IP地址及端口是否在线  
  149. bool ShellProcess::isAlive(QString ip, int port, int timeout)  
  150.  
  151.     QTcpSocket tcpClient;  
  152.     tcpClient.abort();  
  153.     tcpClient.connectToHost(ip, port);  
  154.     //100毫秒没有连接上则判断不在线  
  155.     return tcpClient.waitForConnected(timeout);  
  156. }  

0

阅读 收藏 喜欢 打印举报/Report
后一篇:C++ vector用法
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有