c++ netplan ubuntu yaml 读取和创建

Posted

技术标签:

【中文标题】c++ netplan ubuntu yaml 读取和创建【英文标题】:c++ netplan ubuntu yaml read and create 【发布时间】:2019-01-20 19:05:46 【问题描述】:

我创建了一个 c++ 应用程序,需要读取 Ubuntu Netplan yaml 文件。 我发现很多网站都有很多有用的信息。大多数情况下,一切都不适用于 netplan 文件。它每次都会崩溃,通常是当我将根节点设置为网络时。但是,这是必要的,因为您仍然不知道它是什么网卡。

network.yaml

    network: 
        ethernets: 
          eth0: 
            addresses: 
              - 192.168.0.30/24
            dhcp4: false
            gateway4: "192.168.0.1"
            nameservers: 
              addresses: 
                - "211.211.190.30"
                - "211.160.60.1"
                - "8.8.8.8"
              search: 
                - Network.local
        renderer: networkd
        version: 2

它必须保存到结构中以供其他作品使用。

Parsing yaml with yaml cpp yaml-cpp Easiest way to iterate through a map with undefined values yaml-cpp read sequence in item http://albertocorona.com/yaml-cpp-a-small-tutorial-to-serialization/
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <dirent.h>
#include <yaml-cpp/yaml.h>
#include <yaml-cpp/node/node.h>
#include <yaml-cpp/node/iterator.h>

using namespace std;

static string path02 = "/Netzwerk.yaml";


struct NetInfo 
    int             ID;
    string          version;
    string          renderer;
    string          IF_Type;
    string          name;
    string          dhcp4;
    vector<string>  addresseIF;
    string          gateway4;
    vector<string>  NSsearch;
    vector<string>  NSaddress;
;

int main()
    NetInfo yamlsafe;
    YAML::Node config = YAML::LoadFile(path02);
    string NetzwerkTyp = config["network"]["ethernets"].as<std::string>();
    string NetzManager = config["network"]["renderer"].as<string>();
    string If_Name     = config["network"]["eth0"].as<string>();
    string DHCP4       = config["eth0"]["addresses"].as<string>();
    string IP          = config["eth0"]["dhcp4"].as<string>();
    string Gateway     = config["eth0"]["gateway4"].as<string>();
    string NS_IP       = config["nameservers"]["addresses"].as<string>();
    string NS_Search   = config["nameservers"]["search"].as<string>();

    cout <<"NetzwerkTyp:"  << "\t" << NetzwerkTyp << endl;
    cout <<"Netz Manager:" << "\t" << NetzManager << endl;
    cout <<"If-Name:"      << "\t" << If_Name     << endl;
    cout <<"DHCP4"         << "\t" << DHCP4       << endl;
    cout <<"IP"            << "\t" << IP          << endl;
    cout <<"Gateway"       << "\t" << Gateway     << endl;
    cout <<"NS-IP"         << "\t" << NS_IP       << endl;
    cout <<"NS-Search"     << "\t" << NS_Search   << endl;

    //second test 
    YAML::Node config1 = YAML::LoadFile(path02);
    const YAML::Node& sensors = config1["network"];
    for (YAML::const_iterator it = sensors.begin(); it != sensors.end(); ++it) 
        const YAML::Node& sensor = *it;
        std::cout << "address: " << sensor["addresses"].as<std::string>() << "\n";
        std::cout << "dhcp: " << sensor["dhcp4"].as<std::string>() << "\n";
    
    return 0;


是的,我也有同样的想法,我已经看到了这个提示,但是 没有什么是它所做的。它挂断了QT ide。 我的第二件事是下面的第二个测试。

YAML::Node config1 = YAML::LoadFile(path02); const YAML::Node& node1 = config1["network"]; //network: ------------------------>node2 const YAML::Node& node2 = node1["network"]["ethernets"]; // ethernets: ------------------>node3 const YAML::Node& node3 = node2["ethernets"]["eth0"]; // eth0: --------------------->node4 const YAML::Node& node4 = node3["eth0"]["addresses"]; // addresses: ----------------------N1-> - seq1 const vector& node4s1 = node4["addresses"]; // - 192.168.0.30/24-----------------> - seq1 -p1 const YAML::Node& node4 = node3["eth0"]["dhcp4"]; // dhcp4: false ------------------>node4-2 const YAML::Node& node4 = node3["eth0"]["gateway4"]; // gateway4: "192.168.0.1"-------->node4-3 const YAML::Node& node4 = node3["eth0"]["nameservers"]; // nameservers: ------------------>node4-4 const vector& node4s2 = node4["nameservers"]["addresses"]; // addresses: --------------------N5-> - seq2 // - "211.211.190.30"--------------> - seq2 - p1 // - "211.160.60.1"----------------> - seq2 - p2 // - "8.8.8.8"---------------------> - seq2 - p3 const vector& node4s3 = node4["nameservers"]["search"]; // search: -----------------------N6-> - seq3 // - Network.local-----------------> - seq3 - p1 const YAML::Node& node2 = node1["network"]["renderer"]; // renderer: networkd---------->node5 const YAML::Node& node2 = node1["network"]["version"]; // version: 2------------------>node6

这就是我想要的,但它不起作用。

【问题讨论】:

我不知道在 C++ 中查找对象config 是如何工作的,但我希望config["network"]["eth0"] 完全写成config["network"]["ethernets"]["eth0"],就像在映射中一样network 的值没有键 eth0 是的,我知道这一点,但我只使用节点,该 yaml 文件中的所有前面的单词都标识为节点,我成为了我的输出。 eth0 的问题在于它是网卡的接口,在动态过程中,当一个参考点出现在它自身时是很困难的。我尝到了键和值的味道,但同时该文件键和值的所有点。当我使用映射和序列时,参考它是没有意义的。我也成为键和值的相同输出。我不知道为什么。 【参考方案1】:

好的,这就是我有一个结构并且我可以读取所有节点的答案。

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <dirent.h>
#include <yaml-cpp/yaml.h>
#include <yaml-cpp/node/iterator.h>

using namespace std;

struct DateiLesen 
    string version;
    string renderer;
    string IF_Type;
    string name;
    string dhcp4;
    string dhcp6;
    vector<string> addresseIF;
    string gateway4;
    string gateway6;
    vector<string> NSsearch;
    vector<string> NSaddress;
;
static vector<DateiLesen> DLS2;

static string path = "/QTProjects/YAML-15/test/Netzwerk.yaml";

void Yaml_lesen()
    YAML::Node nodew = YAML::LoadFile(path);
    string NetIf = "eth0";

    DLS2.push_back(DateiLesen());

    if (nodew["network"]) 
        if (nodew["network"]["version"]) 
            DLS2[0].version = nodew["network"]["version"].as<string>();
            //std::cout << nodew["network"]["version"].as<std::string>() << "\n";
        
        if (nodew["network"]["renderer"]) 
            DLS2[0].renderer = nodew["network"]["renderer"].as<std::string>();
            //std::cout << nodew["network"]["renderer"].as<std::string>() << "\n";
        
        if (nodew["network"]["ethernets"]) 
            DLS2[0].IF_Type = "ethernets";
            if (nodew["network"]["ethernets"][NetIf]) 
                if (nodew["network"]["ethernets"][NetIf]["dhcp4"]) 
                    DLS2[0].dhcp4 = nodew["network"]["ethernets"][NetIf]["dhcp4"].as<string>();
                    //std::cout << nodew["network"]["ethernets"][NetIf]["dhcp4"].as<std::string>() << "\n";
                
                if (nodew["network"]["ethernets"][NetIf]["dhcp6"]) 
                    DLS2[0].dhcp6 = nodew["network"]["ethernets"][NetIf]["dhcp6"].as<string>();
                    //std::cout << nodew["network"]["ethernets"][NetIf]["dhcp6"].as<std::string>() << "\n";
                
                if (nodew["network"]["ethernets"][NetIf]["addresses"]) 
                    if (nodew["network"]["ethernets"][NetIf]["addresses"].IsSequence()) 
                        for (unsigned long it = 0; it < nodew["network"]["ethernets"][NetIf]["addresses"].size(); ++it) 
                            DLS2[0].addresseIF.push_back(nodew["network"]["ethernets"][NetIf]["addresses"][it].as<std::string>());
                            //cout << nodew["network"]["ethernets"][NetIf]["addresses"][it].as<std::string>() << "\n";
                        
                    
                
                if (nodew["network"]["ethernets"][NetIf]["gateway4"]) 
                    DLS2[0].gateway4 = nodew["network"]["ethernets"][NetIf]["gateway4"].as<string>();
                    //std::cout << nodew["network"]["ethernets"][NetIf]["gateway4"].as<std::string>() << "\n";
                
                if (nodew["network"]["ethernets"][NetIf]["gateway6"]) 
                    DLS2[0].gateway6 = nodew["network"]["ethernets"][NetIf]["gateway6"].as<string>();
                    //std::cout << nodew["network"]["ethernets"][NetIf]["gateway4"].as<std::string>() << "\n";
                
                if (nodew["network"]["ethernets"][NetIf]["nameservers"]) 
                    //cout << "Nameservers" << endl;
                    if (nodew["network"]["ethernets"][NetIf]["nameservers"]["search"]) 
                        if (nodew["network"]["ethernets"][NetIf]["nameservers"]["search"].IsSequence()) 
                            for (unsigned long it = 0; it < nodew["network"]["ethernets"][NetIf]["nameservers"]["search"].size(); ++it) 
                                DLS2[0].NSsearch.push_back(nodew["network"]["ethernets"][NetIf]["nameservers"]["search"][it].as<std::string>());
                                //cout << nodew["network"]["ethernets"][NetIf]["nameservers"]["search"][it].as<std::string>() << "\n";
                            
                        
                    
                    if (nodew["network"]["ethernets"][NetIf]["nameservers"]["addresses"]) 
                        if (nodew["network"]["ethernets"][NetIf]["nameservers"]["addresses"].IsSequence()) 
                            for (unsigned long it = 0; it < nodew["network"]["ethernets"][NetIf]["nameservers"]["addresses"].size(); ++it) 
                                DLS2[0].NSaddress.push_back(nodew["network"]["ethernets"][NetIf]["nameservers"]["addresses"][it].as<std::string>());
                                //cout << nodew["network"]["ethernets"][NetIf]["nameservers"]["addresses"][it].as<std::string>() << "\n";
                            
                        
                    
                
            
        else if(nodew["network"]["wifis"])
            DLS2[0].IF_Type = "wifis";
            if (nodew["network"]["wifis"][NetIf]) 
                if (nodew["network"]["wifis"][NetIf]["dhcp4"]) 
                    DLS2[0].dhcp4 = nodew["network"]["wifis"][NetIf]["dhcp4"].as<string>();
                    //std::cout << nodew["network"]["wifis"][NetIf]["dhcp4"].as<std::string>() << "\n";
                
                if (nodew["network"]["wifis"][NetIf]["dhcp6"]) 
                    DLS2[0].dhcp6 = nodew["network"]["wifis"][NetIf]["dhcp6"].as<string>();
                    //std::cout << nodew["network"]["wifis"][NetIf]["dhcp6"].as<std::string>() << "\n";
                
                if (nodew["network"]["wifis"][NetIf]["addresses"]) 
                    if (nodew["network"]["wifis"][NetIf]["addresses"].IsSequence()) 
                        for (unsigned long it = 0; it < nodew["network"]["wifis"][NetIf]["addresses"].size(); ++it) 
                            DLS2[0].addresseIF.push_back(nodew["network"]["wifis"][NetIf]["addresses"][it].as<std::string>());
                            //cout << nodew["network"]["wifis"][NetIf]["addresses"][it].as<std::string>() << "\n";
                        
                    
                
                if (nodew["network"]["wifis"][NetIf]["gateway4"]) 
                    DLS2[0].gateway4 = nodew["network"]["wifis"][NetIf]["gateway4"].as<string>();
                    //std::cout << nodew["network"]["wifis"][NetIf]["gateway4"].as<std::string>() << "\n";
                
                if (nodew["network"]["wifis"][NetIf]["gateway6"]) 
                    DLS2[0].gateway6 = nodew["network"]["wifis"][NetIf]["gateway6"].as<string>();
                    //std::cout << nodew["network"]["wifis"][NetIf]["gateway4"].as<std::string>() << "\n";
                
                if (nodew["network"]["wifis"][NetIf]["nameservers"]) 
                    cout << "Nameservers" << endl;
                    if (nodew["network"]["wifis"][NetIf]["nameservers"]["search"]) 
                        if (nodew["network"]["wifis"][NetIf]["nameservers"]["search"].IsSequence()) 
                            for (unsigned long it = 0; it < nodew["network"]["wifis"][NetIf]["nameservers"]["search"].size(); ++it) 
                                DLS2[0].NSsearch.push_back(nodew["network"]["wifis"][NetIf]["nameservers"]["search"][it].as<std::string>());
                                //cout << nodew["network"]["wifis"][NetIf]["nameservers"]["search"][it].as<std::string>() << "\n";
                            
                        
                    
                    if (nodew["network"]["wifis"][NetIf]["nameservers"]["addresses"]) 
                        if (nodew["network"]["wifis"][NetIf]["nameservers"]["addresses"].IsSequence()) 
                            for (unsigned long it = 0; it < nodew["network"]["wifis"][NetIf]["nameservers"]["addresses"].size(); ++it) 
                                DLS2[0].NSaddress.push_back(nodew["network"]["wifis"][NetIf]["nameservers"]["addresses"][it].as<std::string>());
                                //cout << nodew["network"]["wifis"][NetIf]["nameservers"]["addresses"][it].as<std::string>() << "\n";
                            
                        
                    
                
            
        else if(nodew["network"]["bonds"])

        else if(nodew["network"]["bridges"])

        else if(nodew["network"]["vlans"])

        
    


int main()
    Yaml_lesen();

    for (unsigned long S = 0; S < DLS2.size(); S++)
        cout << DLS2[S].renderer << endl;
        cout << DLS2[S].name << endl;
        cout << DLS2[S].IF_Type << endl;
        cout << DLS2[S].dhcp4 << endl;
        for (unsigned long S2 = 0; S2 < DLS2[S].addresseIF.size(); S2++)
            cout << DLS2[S].addresseIF[S2] << endl;
        
        cout << DLS2[S].gateway4 << endl;
        for (unsigned long S2 = 0; S2 < DLS2[S].addresseIF.size(); S2++)
            cout << DLS2[S].NSsearch[S2] << endl;
        
        for (unsigned long S2 = 0; S2 < DLS2[S].addresseIF.size(); S2++)
            cout << DLS2[S].NSaddress[S2] << endl;
        
    

    return 0;

【讨论】:

以上是关于c++ netplan ubuntu yaml 读取和创建的主要内容,如果未能解决你的问题,请参考以下文章

ubuntu18.04netplan命令没有

linux运维Ubuntu18.04 设置网络信息(netplan之yaml文件)

linux运维Ubuntu18.04 设置网络信息(netplan之yaml文件)

linux运维Ubuntu18.04 设置网络信息(netplan之yaml文件)

Ubuntu配置静态IP

Ubuntu 18 LTS netplan 网络配置