shell检测interface是否已分配ip,qt调用shell脚本

#include <QCoreApplication>
#include <QDebug>
#include <QTextStream>
#include <QDir>
#include <QFile>
#include <QList>
#include <QThread>
#include <QtNetwork/QNetworkConfigurationManager>
#include <QtNetwork/QNetworkConfiguration>
#include <QtNetwork/QNetworkInterface>
#define ETH_IF_NAME   "eth0"
#define ETH_IF_PPP0   "ppp0"
#define ETH_IF_PPP1   "ppp1"
#define ETH_IF_ETH1   "eth1"
#define ETH_IF_USB0   "usb0"
#define ETH_NAME_SERVER     "nameserver 127.0.0.1"

 

bool have_ip(char* dev)
{
    char cmd[200]={0};
    sprintf(cmd, "/sbin/ifconfig %s | grep \"inet addr\" | awk '{ print $2}' | awk -F: '{print $2}' > /tmp/ipaddr" ,dev);
    ::system("rm -rf /tmp/ipaddr");
    ::system(cmd);
    qDebug()<<"cmd:"<<cmd<<endl;

    QFile file("/tmp/ipaddr");
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug()<<"Can't open the file!"<<endl;
    }
    QThread::msleep(100);
    QByteArray line = file.readLine();
    QString str(line);
    qDebug()<<"result:"<<str<<endl;
    if(str.length() > 4)
    {
        return true;
    } else {
        return false;
    }

}

 

 

其中的shell:

#!/bin/bash
sleep 8

while [ 1 ]
do
     ipaddr=` /sbin/ifconfig eth0 | grep "inet addr" | awk '{ print $2}' | awk -F: '{print $2}' `
     if [ ! -n "$ipaddr" ]
     then
          echo "NO IP Addr"
     fi 
        sleep 8
done

 

posted @ 2016-11-14 11:34  oucaijun  阅读(473)  评论(0编辑  收藏  举报
下载TeamViewer完整版 下载TeamViewer