如何用qt获取/etc/network/interface文件中的配置信息及写入信息

void read_interface_file()
{
    QFile readFile("/etc/network/interfaces");
    QString strAll;
    if(readFile.open((QIODevice::ReadOnly|QIODevice::Text)))
    {
        QTextStream stream(&readFile);
        strAll=stream.readAll();
    }
    readFile.close();
    QStringList strList;
    strList=strAll.split("\n");
    int eth_index = -1;
    for(int i=0;i<strList.size();i++)
    {
        if(strList.at(i) == "iface eth0 inet static")
        {
            eth_index = 0;
        }
        else if(strList.at(i) == "iface eth1 inet static")
        {
            eth_index = 1;
        }
        else if(strList.at(i).contains("iface eth"))
        {
            eth_index = -1;
        }
        else if(strList.at(i).contains("address"))
        {
            if(eth_index==0)
            {
                ip0 = strList.at(i);
            }
            else if(eth_index==1)
            {
                ip1 = strList.at(i);
            }
        }
        else if(strList.at(i).contains("netmask"))
        {
            if(eth_index==0)
            {
                mask0 = strList.at(i);
            }
            else if(eth_index==1)
            {
                mask1 = strList.at(i);
            }
        }
        else if(strList.at(i).contains("gateway"))
        {
            if(eth_index==0)
            {
                gate0 = strList.at(i);
            }
            else if(eth_index==1)
            {
                gate1 = strList.at(i);
            }
        }

    }

    ip0 = ip0.mid(ip0.indexOf(" ")).replace(" ","");
    mask0 = mask0.mid(mask0.indexOf(" ")).replace(" ","");
    gate0 = gate0.mid(gate0.indexOf(" ")).replace(" ","");
    ip1 = ip1.mid(ip1.indexOf(" ")).replace(" ","");
    mask1 = mask1.mid(mask1.indexOf(" ")).replace(" ","");
    gate1 = gate1.mid(gate1.indexOf(" ")).replace(" ","");
    qDebug()<<"ip0:"<<ip0;
    qDebug()<<"mask0:"<<mask0;
    qDebug()<<"gate0:"<<gate0;
    qDebug()<<"ip1:"<<ip1;
    qDebug()<<"mask1:"<<mask1;
    qDebug()<<"gate1:"<<gate1;
}



void write_interface()
{
    QString strAll;
    QStringList strList;
    QFile readFile("/etc/network/interfaces");
    if(readFile.open((QIODevice::ReadOnly|QIODevice::Text)))
    {
        QTextStream stream(&readFile);
        strAll=stream.readAll();
    }
    readFile.close();
    QFile writeFile("/etc/network/interfaces");

    QString ip;
    QString mask;
    QString gateway;

    if(writeFile.open(QIODevice::WriteOnly))
    {
        QTextStream stream(&writeFile);
        strList=strAll.split("\n");
        qDebug()<<"strList size = "<<strList.count();
        for(int i=0;i<strList.count();i++)
        {
            if(i==strList.count()-1)
            {
                stream<<strList.at(i);
            }
            else
            {
                stream<<strList.at(i)<<'\n';
            }
            if(strList.at(i).contains("iface eth0 inet static"))
            {
                ip.sprintf("address %d.%d.%d.%d",(eth0_ownIpAddr >> 24) & 0xff, (eth0_ownIpAddr >> 16) & 0xff,
                           (eth0_ownIpAddr >> 8) & 0xff, eth0_ownIpAddr & 0xff);
                mask.sprintf("netmask %d.%d.%d.%d",(eth0_SubNetMask >> 24) & 0xff, (eth0_SubNetMask >> 16) & 0xff,
                             (eth0_SubNetMask >> 8) & 0xff, eth0_SubNetMask & 0xff);
                gateway.sprintf("gateway %d.%d.%d.%d",(eth0_GateWay >> 24) & 0xff, (eth0_GateWay >> 16) & 0xff,
                                (eth0_GateWay >> 8) & 0xff, eth0_GateWay & 0xff);


                qDebug()<<"eth0 ip = "<<ip;
                qDebug()<<"eth0 mask = "<<mask;
                qDebug()<<"eth0 gateway = "<<gateway;
                QString tempStr=strList.at(i+1);
                tempStr.replace(0,tempStr.length(),ip);
                qDebug()<<"eth0 ip = "<<ip;

                stream<<tempStr<<'\n';
                tempStr=strList.at(i+2);
                tempStr.replace(0,tempStr.length(),mask);
                qDebug()<<"eth0 mask = "<<mask;
                stream<<tempStr<<'\n';
                tempStr=strList.at(i+3);
                tempStr.replace(0,tempStr.length(),"network 192.168.0.12");
                stream<<tempStr<<'\n';
                tempStr=strList.at(i+4);
                tempStr.replace(0,tempStr.length(),gateway);
                qDebug()<<"eth0 gateway = "<<gateway;
                stream<<tempStr<<'\n';
                i+=4;
            }

            ip.clear();
            mask.clear();
            gateway.clear();
            if(strList.at(i).contains("iface eth1 inet static"))
            {
                ip.sprintf("address %d.%d.%d.%d",(eth1_ownIpAddr >> 24) & 0xff, (eth1_ownIpAddr >> 16) & 0xff,
                           (eth1_ownIpAddr >> 8) & 0xff, eth1_ownIpAddr & 0xff);
                mask.sprintf("netmask %d.%d.%d.%d",(eth1_SubNetMask >> 24) & 0xff, (eth1_SubNetMask >> 16) & 0xff,
                             (eth1_SubNetMask >> 8) & 0xff, eth1_SubNetMask & 0xff);
                gateway.sprintf("gateway %d.%d.%d.%d",(eth1_GateWay >> 24) & 0xff, (eth1_GateWay >> 16) & 0xff,
                                (eth1_GateWay >> 8) & 0xff, eth1_GateWay & 0xff);

                qDebug()<<"eth1 ip = "<<ip;
                qDebug()<<"eth1 mask = "<<mask;
                qDebug()<<"eth1 gateway = "<<gateway;

                QString tempStr=strList.at(i+1);
                tempStr.replace(0,tempStr.length(),ip);
                qDebug()<<"eth1 ip = "<<ip;

                stream<<tempStr<<'\n';
                tempStr=strList.at(i+2);
                tempStr.replace(0,tempStr.length(),mask);
                qDebug()<<"eth1 mask = "<<mask;
                stream<<tempStr<<'\n';
                tempStr=strList.at(i+3);
                tempStr.replace(0,tempStr.length(),"network 192.168.0.12");
                stream<<tempStr<<'\n';
                tempStr=strList.at(i+4);
                tempStr.replace(0,tempStr.length(),gateway);
                qDebug()<<"eth1 gateway = "<<gateway;
                stream<<tempStr<<'\n';
                i+=4;
            }

            ip.clear();
            mask.clear();
            gateway.clear();
            if(strList.at(i).contains("iface eth2 inet static"))
            {
                UINT32 eth2_ip = vos_dottedIP(ip_eth2);
                eth2_ip = htonl(eth2_ip);
                UINT32 eth2_mask = vos_dottedIP(sub_net_mask_eth2);
                eth2_mask = htonl(eth2_mask);
                UINT32 eth2_gate = vos_dottedIP(gate_way_eth2);
                eth2_gate = htonl(eth2_gate);
                ip.sprintf("address %d.%d.%d.%d",(eth2_ip >> 24) & 0xff, (eth2_ip >> 16) & 0xff,
                           (eth2_ip >> 8) & 0xff, eth2_ip & 0xff);
                mask.sprintf("netmask %d.%d.%d.%d",(eth2_mask >> 24) & 0xff, (eth2_mask >> 16) & 0xff,
                             (eth2_mask >> 8) & 0xff, eth2_mask & 0xff);
                gateway.sprintf("gateway %d.%d.%d.%d",(eth2_gate >> 24) & 0xff, (eth2_gate >> 16) & 0xff,
                                (eth2_gate >> 8) & 0xff, eth2_gate & 0xff);

                QString tempStr=strList.at(i+1);
                tempStr.replace(0,tempStr.length(),ip);
                stream<<tempStr<<'\n';
                tempStr=strList.at(i+2);
                tempStr.replace(0,tempStr.length(),mask);
                stream<<tempStr<<'\n';
                tempStr=strList.at(i+3);
                tempStr.replace(0,tempStr.length(),"network 192.168.0.11");
                stream<<tempStr<<'\n';
                tempStr=strList.at(i+4);
                tempStr.replace(0,tempStr.length(),gateway);
                stream<<tempStr<<'\n';
                i+=4;
            }

        }
    }

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值