-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget_netplan_config.cpp
More file actions
139 lines (130 loc) · 6.79 KB
/
get_netplan_config.cpp
File metadata and controls
139 lines (130 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "functions.hpp"
std::string interface = "";
std::string dhcp4;
std::string dhcp6;
std::string ip_address;
std::string routes;
std::string accept_ra;
std::string type = "";
std::string bond_interfaces;
std::string bond_parameters;
bool check_interface = false;
int current_uid = getuid();
int check_configured (std::string interface) {
int is_configured = 0;
std::string check_cmd = "/usr/sbin/netplan get network.ethernets."+interface;
std::string check = exec(check_cmd.c_str());
check.erase(std::remove(check.begin(), check.end(), '\n'), check.end());
if (check == "null") {
is_configured = 1;
}
return is_configured;
}
int main (int argc, char **argv) {
try {
boost::program_options::options_description desc{"Options"};
desc.add_options()
("help,h", "Help screen")
("interface", boost::program_options::value<std::string>()->required(), "Name of the interface (e.g. eth0)")
("type", boost::program_options::value<std::string>()->required(), "Type of interface (ethernets || bonds)")
("check_configured", "Check if configuration for this interface exists)");
boost::program_options::variables_map vm;
boost::program_options::command_line_style::allow_long_disguise;
store(parse_command_line(argc, argv, desc), vm);
if (vm.count("help")) {
std::cout << desc << '\n';
return 0;
}
notify(vm);
if (vm.count("interface")) {
interface = vm["interface"].as<std::string>();
}
if (vm.count("type")) {
type = vm["type"].as<std::string>();
}
if (vm.count("check_configured")) {
check_interface = true;
int result = check_configured (interface);
}
}
catch (const boost::program_options::error &ex) {
std::cerr << ex.what() << '\n';
return 1;
}
if (type != "ethernets" && type != "bonds") {
std::cerr << "Error! Unknown interface type!" << std::endl;
return 1;
}
if (setuid(0)) { //I am now root!
perror("setuid");
return 1;
}
if (!check_interface) {
std::string dhcp4_cmd = "/usr/sbin/netplan get network."+type+"."+interface+".dhcp4";
dhcp4 = exec(dhcp4_cmd.c_str());
//remove unwanted chars from string (last \n, -, spaces and '"')
dhcp4.pop_back();
dhcp4.erase(std::remove(dhcp4.begin(), dhcp4.end(), '-'), dhcp4.end());
dhcp4.erase(std::remove(dhcp4.begin(), dhcp4.end(), ' '), dhcp4.end());
dhcp4.erase(std::remove(dhcp4.begin(), dhcp4.end(), '"'), dhcp4.end());
replaceAll(dhcp4, "\n", ", ");
std::string dhcp6_cmd = "/usr/sbin/netplan get network."+type+"."+interface+".dhcp6";
dhcp6 = exec(dhcp6_cmd.c_str());
//remove unwanted chars from string (last \n, -, spaces and '"')
dhcp6.pop_back();
dhcp6.erase(std::remove(dhcp6.begin(), dhcp6.end(), '-'), dhcp6.end());
dhcp6.erase(std::remove(dhcp6.begin(), dhcp6.end(), ' '), dhcp6.end());
dhcp6.erase(std::remove(dhcp6.begin(), dhcp6.end(), '"'), dhcp6.end());
replaceAll(dhcp6, "\n", ", ");
std::string ip_address_cmd = "/usr/sbin/netplan get network."+type+"."+interface+".addresses";
ip_address = exec(ip_address_cmd.c_str());
//remove unwanted chars from string (last \n, -, spaces and '"')
ip_address.pop_back();
ip_address.erase(std::remove(ip_address.begin(), ip_address.end(), '-'), ip_address.end());
ip_address.erase(std::remove(ip_address.begin(), ip_address.end(), ' '), ip_address.end());
ip_address.erase(std::remove(ip_address.begin(), ip_address.end(), '"'), ip_address.end());
replaceAll(ip_address, "\n", ", ");
std::string routes_cmd = "/usr/sbin/netplan get network."+type+"."+interface+".routes";
routes = exec(routes_cmd.c_str());
//remove unwanted chars from string (last \n, -, spaces and '"')
routes.pop_back();
routes.erase(std::remove(routes.begin(), routes.end(), '-'), routes.end());
routes.erase(std::remove(routes.begin(), routes.end(), ' '), routes.end());
routes.erase(std::remove(routes.begin(), routes.end(), '"'), routes.end());
replaceAll(routes, "\n", ", ");
std::string accept_ra_cmd = "/usr/sbin/netplan get network."+type+"."+interface+".accept-ra";
accept_ra = exec(accept_ra_cmd.c_str());
//remove unwanted chars from string (last \n, -, spaces and '"')
accept_ra.pop_back();
accept_ra.erase(std::remove(accept_ra.begin(), accept_ra.end(), '-'), accept_ra.end());
accept_ra.erase(std::remove(accept_ra.begin(), accept_ra.end(), ' '), accept_ra.end());
accept_ra.erase(std::remove(accept_ra.begin(), accept_ra.end(), '"'), accept_ra.end());
replaceAll(accept_ra, "\n", ", ");
if (type == "bonds") {
std::string interfaces_cmd = "/usr/sbin/netplan get network.bonds."+interface+".interfaces";
bond_interfaces = exec(interfaces_cmd.c_str());
//remove unwanted chars from string (last \n, -, spaces and '"')
bond_interfaces.pop_back();
bond_interfaces.erase(std::remove(bond_interfaces.begin(), bond_interfaces.end(), '-'), bond_interfaces.end());
bond_interfaces.erase(std::remove(bond_interfaces.begin(), bond_interfaces.end(), ' '), bond_interfaces.end());
bond_interfaces.erase(std::remove(bond_interfaces.begin(), bond_interfaces.end(), '"'), bond_interfaces.end());
replaceAll(bond_interfaces, "\n", ", ");
std::string bond_parameters_cmd = "/usr/sbin/netplan get network.bonds."+interface+".parameters";
bond_parameters = exec(bond_parameters_cmd.c_str());
//remove unwanted chars from string (last \n, -, spaces and '"')
bond_parameters.pop_back();
//bond_parameters.erase(std::remove(bond_parameters.begin(), bond_parameters.end(), '-'), bond_parameters.end());
bond_parameters.erase(std::remove(bond_parameters.begin(), bond_parameters.end(), ' '), bond_parameters.end());
bond_parameters.erase(std::remove(bond_parameters.begin(), bond_parameters.end(), '"'), bond_parameters.end());
replaceAll(bond_parameters, "\n", "; ");
replaceAll(bond_parameters, "arp-ip-targets:;", "arp-ip-targets:");
replaceAll(bond_parameters, "; -", ",");
}
setuid(current_uid); //return to previous user
std::cout<<"dhcp4: "<<dhcp4<<std::endl<<"dhcp6: "<<dhcp6<<std::endl<<"ip_address: "<<ip_address<<std::endl<<"routes: "<<routes<<std::endl<<"accept_ra: "<<accept_ra<<std::endl;
if (type == "bonds") {
std::cout<<"interfaces: "<<bond_interfaces<<std::endl<<"parameters: "<<bond_parameters<<std::endl;
}
}
return 0;
}