c++ netplan ubuntu yaml 读取和创建

c++ netplan ubuntu yaml read and create

我已经创建了一个 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

它必须保存到另一个作品的结构中。


#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


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

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

</p> <pre><code>#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; }