我创建了一个 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
它必须保存到另一个作品的结构中。
- 使用 yaml cpp 解析 yaml
- yaml-cpp 遍历未定义值的地图的最简单方法
- yaml-cpp 读取项目中的序列
- 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
这就是我的想法,但它不起作用。