[修改]实现读取DHCP模式并判断,优化service中读取方法
This commit is contained in:
@@ -39,12 +39,20 @@ class ManageNetwork extends SettingsPage
|
||||
|
||||
// 当选择框的值更新时,自动填充对应的网络信息
|
||||
if (isset($interfaces[$state])) {
|
||||
$dhcp_status = NetworkService::getDHCPstatus($state);
|
||||
$set('dhcp_enabled', $dhcp_status ? 'true':'false');
|
||||
$set('ip', $interfaces[$state]['ip'] ?? '无IP地址');
|
||||
$set('mask', $interfaces[$state]['mask'] ?? '无子网掩码');
|
||||
// 假设我们通过其他方式获取网关(如手动输入或自动获取)
|
||||
// $set('gateway', '自动获取或手动输入的值');
|
||||
$set('gateway', $interfaces[$state]['gateway']);
|
||||
$set('dns_server_1', $interfaces[$state]['dns'][0] ?? null);
|
||||
$set('dns_server_2', $interfaces[$state]['dns'][1] ?? null);
|
||||
|
||||
error_log('state:' . $state);
|
||||
error_log('dhcp_enabled: ' . $dhcp_status);
|
||||
error_log("ip " . $interfaces[$state]['ip']);
|
||||
error_log("mask " . $interfaces[$state]['mask']);
|
||||
error_log("gateway " . $interfaces[$state]['gateway']);
|
||||
error_log("dns_server_1 " . print_r($interfaces[$state]['dns'], true));
|
||||
}
|
||||
else{
|
||||
error_log("未找到接口:" . $state);
|
||||
@@ -54,84 +62,67 @@ class ManageNetwork extends SettingsPage
|
||||
// 动态显示端口信息
|
||||
Fieldset::make('端口信息')
|
||||
->schema([
|
||||
Select::make('dhcp_enabled')
|
||||
->label('DHCP启用')
|
||||
->options([
|
||||
'true' => '是',
|
||||
'false' => '否',
|
||||
])
|
||||
->reactive()
|
||||
->required(), // 如果需要可以加上这个
|
||||
TextInput::make('ip')
|
||||
->label("IP地址")
|
||||
->readOnly() // 禁用以表示这是自动填充的字段
|
||||
->disabled(fn($get) => $get('dhcp_enabled') === 'true')
|
||||
->reactive(),
|
||||
TextInput::make('mask')
|
||||
Select::make('mask')
|
||||
->label('子网掩码')
|
||||
->readOnly()
|
||||
->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()
|
||||
->disabled(fn($get) => $get('dhcp_enabled') === 'true')
|
||||
->reactive(),
|
||||
TextInput::make('gateway')
|
||||
->label('网关')
|
||||
->disabled(fn($get) => $get('dhcp_enabled') === 'true')
|
||||
->required(),
|
||||
TextInput::make('dns_server_1')
|
||||
->label('DNS地址')
|
||||
->disabled(fn($get) => $get('dhcp_enabled') === 'true')
|
||||
->required(),
|
||||
TextInput::make('dns_server_2')
|
||||
->label('DNS备用地址')
|
||||
->disabled(fn($get) => $get('dhcp_enabled') === 'true')
|
||||
->required(),
|
||||
])
|
||||
->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])),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -148,17 +139,17 @@ class ManageNetwork extends SettingsPage
|
||||
{
|
||||
// 获取用户输入的网络配置
|
||||
$networkInterface = $this->form->getState()['network_interface'] ?? '';
|
||||
$dhcpEnabled = $this->form->getState()['dhcp_enabled'] ?? false;
|
||||
$dhcpEnabled = $this->form->getState()['dhcp_enabled'];
|
||||
|
||||
error_log('接口名称:' . $networkInterface);
|
||||
error_log('dhcp:' . $dhcpEnabled);
|
||||
|
||||
$ipAddress = $this->form->getState()['ip_address'] ?? '';
|
||||
$subnetMask = $this->form->getState()['subnet_mask'] ?? '';
|
||||
$mask = $this->form->getState()['mask'] ?? '';
|
||||
$gateway = $this->form->getState()['gateway'] ?? '';
|
||||
$dns1 = $this->form->getState()['dns_server_1'] ?? '';
|
||||
$dns2 = $this->form->getState()['dns_server_2'] ?? '';
|
||||
if ($dhcpEnabled) {
|
||||
if ($dhcpEnabled=='true') {
|
||||
|
||||
// 如果启用 DHCP,设置为使用 DHCP
|
||||
$commands = [
|
||||
@@ -185,7 +176,7 @@ class ManageNetwork extends SettingsPage
|
||||
} else {
|
||||
// 根据接口和用户输入构建命令
|
||||
$commands = [
|
||||
"nmcli con mod '$networkInterface' ipv4.method manual ipv4.addresses $ipAddress/$subnetMask ipv4.gateway $gateway ipv4.dns '$dns1 $dns2'",
|
||||
"nmcli con mod '$networkInterface' ipv4.method manual ipv4.addresses $ipAddress/$mask ipv4.gateway $gateway ipv4.dns '$dns1 $dns2'",
|
||||
"nmcli con down '$networkInterface'",
|
||||
"nmcli con up '$networkInterface'", // 重新激活连接
|
||||
];
|
||||
|
||||
@@ -6,8 +6,8 @@ class NetworkService
|
||||
{
|
||||
public static function getNetworkInterfaces()
|
||||
{
|
||||
// 使用 `ifconfig` 来获取接口和 IP 地址
|
||||
$output = shell_exec('ifconfig');
|
||||
// 使用 `nmcli dev show` 来获取接口信息
|
||||
$output = shell_exec('nmcli dev show');
|
||||
return self::parseNetworkInfo($output);
|
||||
}
|
||||
|
||||
@@ -19,20 +19,30 @@ class NetworkService
|
||||
|
||||
foreach ($lines as $line) {
|
||||
// 匹配接口名称
|
||||
if (preg_match('/^([a-zA-Z0-9-]+): flags=/', $line, $matches)) {
|
||||
$interface = $matches[1];
|
||||
if (preg_match('/^GENERAL.DEVICE: *(.+)$/', $line, $matches)) {
|
||||
$interface = trim($matches[1]);
|
||||
$interfaces[$interface] = [
|
||||
'ip' => null,
|
||||
'mask' => null,
|
||||
'gateway' => null,
|
||||
'dns' => [],
|
||||
];
|
||||
}
|
||||
// 匹配 IPv4 地址
|
||||
if ($interface && preg_match('/inet ([\d\.]+)/', $line, $matches)) {
|
||||
$interfaces[$interface]['ip'] = $matches[1];
|
||||
if ($interface && preg_match('/IP4.ADDRESS\[1\]: *([\d\.]+)\/(\d+)/', $line, $matches)) {
|
||||
$ip = $matches[1];
|
||||
$prefix = $matches[2];
|
||||
// $mask = self::prefixToMask($prefix); // 获取子网掩码
|
||||
$interfaces[$interface]['ip'] = $ip;
|
||||
$interfaces[$interface]['mask'] = $prefix; // 设置掩码格式
|
||||
}
|
||||
// 匹配子网掩码
|
||||
if ($interface && preg_match('/netmask ([\d\.]+)/', $line, $matches)) {
|
||||
$interfaces[$interface]['mask'] = $matches[1];
|
||||
// 匹配网关
|
||||
if ($interface && preg_match('/IP4.GATEWAY: *([\d\.]+)/', $line, $matches)) {
|
||||
$interfaces[$interface]['gateway'] = $matches[1];
|
||||
}
|
||||
// 匹配 DNS
|
||||
if ($interface && preg_match('/IP4.DNS\[\d+\]: *([\d\.]+)/', $line, $matches)) {
|
||||
$interfaces[$interface]['dns'][] = $matches[1];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,4 +52,19 @@ class NetworkService
|
||||
return $interfaces;
|
||||
}
|
||||
|
||||
public static function getDHCPstatus($state)
|
||||
{
|
||||
// 调试:检查传递的 state 参数
|
||||
error_log("正在获取DHCP状态的接口: " . $state);
|
||||
|
||||
// 执行 nmcli 命令,获取指定连接的详细信息
|
||||
$output = shell_exec('nmcli con show ' . escapeshellarg($state));
|
||||
|
||||
// 使用正则表达式匹配 'ipv4.method: auto'
|
||||
if (preg_match('/ipv4\.method:\s+auto/', $output)) {
|
||||
return true; // 如果匹配到,说明启用了 DHCP
|
||||
} else {
|
||||
return false; // 否则未启用 DHCP
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user