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     }
  • 相关阅读:
    java单例类
    java构造方法-this关键字的用法
    java封装
    Bean的装配方式
    scope的范围
    Bean实例化(三种方法)
    依赖注入
    建立Spring项目的基础
    Androids中数据库的使用SQLite
    SharedPreferences
  • 原文地址:https://www.cnblogs.com/lizhi0755/p/3029981.html
Copyright © 2011-2022 走看看