[修改]修改并基本实现了网络配置功能

This commit is contained in:
makotocc0107
2024-09-26 16:37:52 +08:00
committed by Coding
parent 491b2265d2
commit d0b379eab1
2 changed files with 106 additions and 4 deletions

View File

@@ -14,8 +14,8 @@ services:
network_mode: host
panel:
build: management-panel
# volumes:
# - ./management-panel:/app
volumes:
- '/var/run/dbus/system_bus_socket:/var/run/dbus/system_bus_socket'
entrypoint: /app/entrypoint.sh
command: php artisan octane:start --server=swoole --workers=4 --host=0.0.0.0 --port=8000
ports:
@@ -25,6 +25,7 @@ services:
- prometheus
restart: always
network_mode: host
privileged: true
node-exporter:
build: docker/node_exporter
# privileged: true

View File

@@ -85,9 +85,35 @@ class ManageNetwork extends SettingsPage
->placeholder('如127.0.0.1')
->required(),
TextInput::make('subnet_mask')
Select::make('subnet_mask')
->label('子网掩码')
->placeholder('如255.255.255.0')
->options([
"8" => "255.0.0.0 /8",
"9" => "255.128.0.0 /9",
"10" => "255.192.0.0 /10",
"11" => "255.224.0.0 /11",
"12" => "255.240.0.0 /12",
"13" => "255.248.0.0 /13",
"14" => "255.252.0.0 /14",
"15" => "255.254.0.0 /15",
"16" => "255.255.0.0 /16",
"17" => "255.255.128.0 /17",
"18" => "255.255.192.0 /18",
"19" => "255.255.224.0 /19",
"20" => "255.255.240.0 /20",
"21" => "255.255.248.0 /21",
"22" => "255.255.252.0 /22",
"23" => "255.255.254.0 /23",
"24" => "255.255.255.0 /24",
"25" => "255.255.255.128 /25",
"26" => "255.255.255.192 /26",
"27" => "255.255.255.224 /27",
"28" => "255.255.255.240 /28",
"29" => "255.255.255.248 /29",
"30" => "255.255.255.252 /30",
"31" => "255.255.255.254 /31",
"32" => "255.255.255.255 /32",
])
->required(),
TextInput::make('gateway')
@@ -108,4 +134,79 @@ class ManageNetwork extends SettingsPage
->visible(fn($get) => in_array($get('dhcp_enabled'), [false])),
]);
}
public function save(): void
{
// 调用父类的保存方法以持久化设置
parent::save();
// 保存后执行终端命令
$this->executeCommands();
}
protected function executeCommands(): void
{
// 获取用户输入的网络配置
$networkInterface = $this->form->getState()['network_interface'] ?? '';
$dhcpEnabled = $this->form->getState()['dhcp_enabled'] ?? false;
error_log('接口名称:' . $networkInterface);
error_log('dhcp' . $dhcpEnabled);
$ipAddress = $this->form->getState()['ip_address'] ?? '';
$subnetMask = $this->form->getState()['subnet_mask'] ?? '';
$gateway = $this->form->getState()['gateway'] ?? '';
$dns1 = $this->form->getState()['dns_server_1'] ?? '';
$dns2 = $this->form->getState()['dns_server_2'] ?? '';
if ($dhcpEnabled) {
// 如果启用 DHCP设置为使用 DHCP
$commands = [
"nmcli con mod '$networkInterface' ipv4.method auto ipv4.addresses \"\" ipv4.gateway \"\" ipv4.dns \"\"",
"nmcli con down '$networkInterface'",
"nmcli con up '$networkInterface'"
];
foreach ($commands as $command) {
$output = [];
$returnVar = 0;
exec($command, $output, $returnVar);
// 记录输出和返回状态以便调试
error_log("命令: $command");
error_log("输出: " . implode("\n", $output));
error_log("返回状态: $returnVar");
// 如果需要,处理错误
if ($returnVar !== 0) {
error_log("执行命令出错: $command");
}
}
} else {
// 根据接口和用户输入构建命令
$commands = [
"nmcli con mod '$networkInterface' ipv4.method manual ipv4.addresses $ipAddress/$subnetMask ipv4.gateway $gateway ipv4.dns '$dns1 $dns2'",
"nmcli con down '$networkInterface'",
"nmcli con up '$networkInterface'", // 重新激活连接
];
foreach ($commands as $command) {
$output = [];
$returnVar = 0;
exec($command, $output, $returnVar);
// 记录输出和返回状态以便调试
error_log("命令: $command");
error_log("输出: " . implode("\n", $output));
error_log("返回状态: $returnVar");
// 如果需要,处理错误
if ($returnVar !== 0) {
error_log("执行命令出错: $command");
}
}
}
}
}