SOUI官方论坛

 找回密码
 立即注册
查看: 1544|回复: 1

ubuntu18.04 C代码控制网络配置

[复制链接]
  • TA的每日心情
    慵懒
    2024-1-24 14:08
  • 签到天数: 313 天

    [LV.8]以坛为家I

    13

    主题

    114

    帖子

    6011

    积分

    08:00大乘期

    Rank: 8Rank: 8

    积分
    6011
    发表于 2019-8-29 18:22:15 | 显示全部楼层 |阅读模式
    本帖最后由 xiaoke 于 2019-8-29 18:27 编辑
    1. //echo "# Let NetworkManager manage all devices on this system:" >/etc/netplan/test.yaml
    2.     cmd1="sudo echo "# Let NetworkManager manage all devices on this system:" >";// 描述信息
    3.     cmd1 += filepath;
    4.         r = this->SystemCommand(cmd1, value);
    5.         printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());

    6.         cmd1="sudo sed -i '$a network:' ";
    7.         cmd1 += filepath;
    8.         r = this->SystemCommand(cmd1, value);
    9.         printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());

    10.         cmd1="sudo sed -i '$a version: 2' ";// 本版信息
    11.         cmd1 += filepath;
    12.         r = this->SystemCommand(cmd1, value);
    13.         printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());

    14.         
    15.         cmd1="sudo sed -i '$a renderer: NetworkManager' ";// 管理信息
    16.         cmd1 += filepath;
    17.         r = this->SystemCommand(cmd1, value);
    18.         printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());

    19.         cmd1="sudo sed -i '$a ethernets:' ";// 修改网卡信息
    20.         cmd1 += filepath;
    21.         r = this->SystemCommand(cmd1, value);
    22.         printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());

    23.     cmd1="sudo sed -i '3,$s/^/  /g' ";// 插入空格
    24.         cmd1 += filepath;
    25.         r = this->SystemCommand(cmd1, value);
    26.         printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());

    27.         int num = 0;
    28.         int nrow = 5;
    29.         for(int i=0;i<m_netinfos.size();i++)
    30.         {
    31.         
    32.             cmd1="sudo sed -i '$a         ";
    33.             cmd1 += m_netinfos.strName;
    34.             cmd1+=":\' ";
    35.                 cmd1 += filepath;
    36.             r = this->SystemCommand(cmd1, value);
    37.             printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());
    38.                 nrow=nrow+1;

    39.         //std::string strNum=lexical_cast<string>(num*3+6);
    40.         ostringstream ostr;
    41.         //ostr<<(num*6+6);
    42.         ostr<<((int)nrow);
    43.     std::string strNum=ostr.str();
    44.         cmd1="sudo sed -i '";
    45.         cmd1 +=strNum;
    46.         cmd1 +=",$s/^/    /g' ";// 插入空格
    47.         cmd1 += filepath;
    48.         r = this->SystemCommand(cmd1, value);
    49.         printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());

    50.     if(m_netinfos.strDhcp4!="")// DHCP
    51.         {
    52.                 cmd1="sudo sed -i '$a          dhcp4: ";
    53.             cmd1 += m_netinfos.strDhcp4;
    54.             cmd1 += "\' ";
    55.                 cmd1 += filepath;
    56.             r = this->SystemCommand(cmd1, value);
    57.             printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());
    58.                 nrow=nrow+1;
    59.         ostr.str("");
    60.         //ostr<<(num*6+6+1);
    61.         ostr<<((int)nrow);
    62.     strNum=ostr.str();
    63.         cmd1="sudo sed -i '";
    64.         cmd1 +=strNum;
    65.         cmd1 +=",$s/^/      /g' ";// 插入空格
    66.         cmd1 += filepath;
    67.         r = this->SystemCommand(cmd1, value);
    68.         printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());
    69.         }

    70.     if(m_netinfos.strIp!="")
    71.         {
    72.                 ostr.str("");// ip/掩码
    73.             ostr<<(netmask_str2len(m_netinfos.strSubMask.c_str()));
    74.         strNum=ostr.str();

    75.             cmd1="sudo sed -i '$a          addresses: [";
    76.             cmd1 += m_netinfos.strIp;
    77.             cmd1 += "/"+strNum+"]\' ";
    78.                 cmd1 += filepath;
    79.             r = this->SystemCommand(cmd1, value);
    80.             printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());
    81.                 nrow=nrow+1;
    82.         ostr.str("");
    83.         //ostr<<(num*6+6+1);
    84.         ostr<<((int)nrow);
    85.     strNum=ostr.str();
    86.         cmd1="sudo sed -i '";
    87.         cmd1 +=strNum;
    88.         cmd1 +=",$s/^/      /g' ";// 插入空格
    89.         cmd1 += filepath;
    90.         r = this->SystemCommand(cmd1, value);
    91.         printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());
    92.         }

    93.     if(m_netinfos.strgateway!="")// gateway4
    94.         {
    95.             cmd1="sudo sed -i \'$a         gateway4: ";
    96.             cmd1+=m_netinfos.strgateway;
    97.             cmd1+="\' ";
    98.                 cmd1 += filepath;
    99.             r = this->SystemCommand(cmd1, value);
    100.             printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());
    101.                 nrow=nrow+1;

    102.         ostr.str("");
    103.         //ostr<<(num*6+6+1);
    104.         ostr<<((int)nrow);
    105.     strNum=ostr.str();
    106.         cmd1="sudo sed -i '";
    107.         cmd1 +=strNum;
    108.         cmd1 +=",$s/^/      /g' ";// 插入空格
    109.         cmd1 += filepath;
    110.         r = this->SystemCommand(cmd1, value);
    111.         printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());
    112.         }

    113.     if(m_netinfos.strDns!="")// // DNS
    114.         {
    115.             cmd1="sudo sed -i '$a nameservers:' ";
    116.                 cmd1 += filepath;
    117.             r = this->SystemCommand(cmd1, value);
    118.             printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());
    119.                 nrow=nrow+1;

    120.         //strNum=lexical_cast<string>(num*3+6+1);
    121.         ostr.str("");
    122.         //ostr<<(num*6+6+1);
    123.         ostr<<((int)nrow);
    124.     strNum=ostr.str();
    125.         cmd1="sudo sed -i '";
    126.         cmd1 +=strNum;
    127.         cmd1 +=",$s/^/      /g' ";// 插入空格
    128.         cmd1 += filepath;
    129.         r = this->SystemCommand(cmd1, value);
    130.         printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());
    131.         
    132.             cmd1="sudo sed -i '$a          addresses: [";               
    133.             cmd1 += m_netinfos.strDns;
    134.             cmd1 += "]\' ";
    135.                 cmd1 += filepath;
    136.             r = this->SystemCommand(cmd1, value);
    137.             printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());
    138.                 nrow=nrow+1;
    139.         
    140.         ostr.str("");
    141.         //ostr<<(num*6+6+5);
    142.         ostr<<((int)nrow);
    143.     strNum=ostr.str();
    144.         cmd1="sudo sed -i '";
    145.         cmd1 +=strNum;
    146.         cmd1 +=",$s/^/        /g' ";// 插入空格
    147.         cmd1 += filepath;
    148.         r = this->SystemCommand(cmd1, value);
    149.         printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());

    150.         }   
    151.                  //num++;
    152.     }

    153.         cmd1="sudo netplan apply";       // 执行生效
    154.         r = this->SystemCommand(cmd1, value);
    155.         printf("SystemCommand: cmd1=%s -------xiaoke\n", cmd1.c_str());
    复制代码

  • TA的每日心情
    开心
    2021-3-1 14:38
  • 签到天数: 1 天

    [LV.1]初来乍到

    0

    主题

    2

    帖子

    27

    积分

    22:00筑基期

    Rank: 1

    积分
    27
    发表于 2021-3-1 14:45:26 | 显示全部楼层
    @@@@LIHAI  厉害厉害
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|小黑屋|SOUI官方论坛

    GMT+8, 2024-5-4 07:42

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表