schema([ // 添加网络接口选择框 Select::make('network_interface') ->label('选择网络接口') ->options(array_combine(array_keys($interfaces), array_keys($interfaces))) ->reactive() ->afterStateUpdated(function ($state, callable $set) use ($interfaces) { // 调试输出:检查当前选择的接口 error_log("选择的接口: " . $state); // 当选择框的值更新时,自动填充对应的网络信息 if (isset($interfaces[$state])) { $set('ip', $interfaces[$state]['ip'] ?? '无IP地址'); $set('mask', $interfaces[$state]['mask'] ?? '无子网掩码'); // 假设我们通过其他方式获取网关(如手动输入或自动获取) // $set('gateway', '自动获取或手动输入的值'); error_log("ip " . $interfaces[$state]['ip']); error_log("mask " . $interfaces[$state]['mask']); } else{ error_log("未找到接口:" . $state); } }), // 动态显示端口信息 Fieldset::make('端口信息') ->schema([ TextInput::make('ip') ->label("IP地址") ->readOnly() // 禁用以表示这是自动填充的字段 ->reactive(), TextInput::make('mask') ->label('子网掩码') ->readOnly() ->reactive(), ]) ->visible(fn($get) => !is_null($get('network_interface'))) // 检查是否有选择的接口 ->reactive(), // DHCP选项 Select::make('dhcp_enabled') ->options([ true => "是", false => "否" ]) ->default(false) ->label("DHCP启用") ->required() ->reactive(), // 动态显示的网络信息字段 Fieldset::make('网络配置') ->schema([ TextInput::make('ip_address') ->label("IP地址") ->placeholder('如127.0.0.1') ->required(), Select::make('subnet_mask') ->label('子网掩码') ->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') ->label('网关') ->placeholder('如192.168.1.1') ->required(), TextInput::make('dns_server_1') ->label('DNS地址') ->placeholder('如6.6.6.6') ->required(), TextInput::make('dns_server_2') ->label('DNS备用地址') ->placeholder('如114.114.114.114') ->required(), ]) ->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"); } } } } }