zoukankan      html  css  js  c++  java
  • 程序调用shell设置ip地址

    通过脚本设置ip地址和网关:

     1 std::string setip = "ifconfig eth0 " +lineEdit_NetIp->text().toStdString() + " netmask " + lineEdit_NetMask->text().toStdString() + " && echo 1";
     2     std::string setroute = "echo 1;route del default;route add default gw " + ui->lineEdit_NetGate->text().toStdString();
     3     FILE *fp = popen(setip.c_str(), "r");
     4     if (NULL == fp)
     5     {
     6         msgBox.setInformativeText(tr("The IP address is unusable!"));
     7         msgBox.exec();
     8         return;
     9     }
    10     char getway[16] = {0};
    11     if (NULL == fgets(getway, 16, fp))
    12     {
    13         pclose(fp);
    14         msgBox.setInformativeText(tr("The IP address is unusable!"));
    15         msgBox.exec();
    16         return;
    17     }
    18     if (getway[0] != '1')
    19     {
    20         msgBox.setInformativeText(tr("The IP address is unusable!"));
    21         msgBox.exec();
    22         return;
    23     }    
    24     pclose(fp);
    25     fp = NULL;
     1 fp = popen(setroute.c_str(), "r");
     2     if (NULL == fp)
     3     {
     4         msgBox.setInformativeText(tr("The route address is unusable!"));
     5        msgBox.exec();
     6         exit(0);
     7    }
     8 
     9    char route[16] = {0};
    10 
    11    if (NULL == fgets(route, 16, fp))
    12    {
    13         pclose(fp);
    14         msgBox.setInformativeText(tr("The route address is unusable!"));
    15         msgBox.exec();
    16         exit(0);
    17    }
    18     pclose(fp);
    19 
    20     if (route[1] != 0)
    21    {
    22        msgBox.setInformativeText(tr("The route address is unusable!"));
    23         msgBox.exec();
    24         exit(0);
    25     }
  • 相关阅读:
    verilog RTL编程实践之四
    TB平台搭建之二
    hdu3466 Proud Merchants
    poj2411 Mondriaan's Dream (用1*2的矩形铺)
    zoj3471 Most Powerful
    poj2923 Relocation
    hdu3001 Travelling
    poj3311 Hie with the Pie
    poj1185 炮兵阵地
    poj3254 Corn Fields
  • 原文地址:https://www.cnblogs.com/lizhi0755/p/3029981.html
Copyright © 2011-2022 走看看