您的位置:首页 > 运维架构 > Shell

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

2016-11-14 11:34 741 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: