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:
#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
相关文章推荐
- SHELL脚本:检测一坨IP是否都能ping通(网卡监控)
- (转)SHELL脚本:检测一坨IP是否都能ping通(网卡监控)
- [shell]通过ping检测整个网段IP的网络状态脚本
- shell脚本检测网络是否畅通
- shell检测输入的IP是否合法
- shell until循环ping检测ip脚本
- 使用shell脚本curl调用jenkins进行构建并判断是否构建成功
- 检测mysql主从复制是否正常的shell脚本
- 调用shell脚本,IP处理
- Shell脚本实现判断IP地址是否在一个ip段内代码分享
- qt中调用shell脚本并监控返回的方法
- Shell脚本实现检测进程是否正在运行
- Shell脚本判断IP是否合法性(多种方法)
- [shell]通过ping检测整个网段IP的网络状态脚本
- shell脚本检测主机是否存活
- Shell脚本实现检测文件是否被修改过代码分享
- 判断IP是否正确的shell 脚本
- Shell脚本实现检测文件是否被修改过代码分享
- WyBox使用shell脚本检测io口是否正常