[增添]增加了网络配置服务,自动读取网络接口及对应信息

This commit is contained in:
makotocc0107
2024-09-24 16:53:57 +08:00
committed by Coding
parent 167d3c7db3
commit eb3f49f00f
4 changed files with 95 additions and 4 deletions

View File

@@ -22,6 +22,7 @@ services:
- etcd
- prometheus
restart: always
network_mode: host
node-exporter:
build: docker/node_exporter
# privileged: true

View File

@@ -5,7 +5,7 @@ FROM openeuler/openeuler:24.03 AS dev
# 更新系统和安装软件包
RUN dnf update -y && \
dnf install -y git php-cli php-bcmath php-mbstring php-pdo php-pecl-zip php-opcache php-sodium php-intl php-pear php-devel c-ares-devel libcurl-devel openssl-devel brotli brotli-devel && \
dnf install -y git php-cli php-bcmath php-mbstring php-pdo php-pecl-zip php-opcache php-sodium php-intl php-pear php-devel c-ares-devel libcurl-devel openssl-devel brotli brotli-devel net-tools && \
dnf clean all && \
rm -rf /var/cache/dnf/*
@@ -43,7 +43,7 @@ RUN npm config set registry https://registry.npmmirror.com && \
FROM openeuler/openeuler:24.03 AS runtime
RUN dnf update -y \
&& dnf install php-cli php-bcmath php-mbstring php-pdo php-pecl-zip php-posix php-sodium php-xml php-intl php-opcache c-ares libcurl openssl brotli -y \
&& dnf install php-cli php-bcmath php-mbstring php-pdo php-pecl-zip php-posix php-sodium php-xml php-intl php-opcache c-ares libcurl openssl brotli net-tools -y \
&& dnf clean all && rm -rf /var/cache/dnf/*
RUN mkdir -p /usr/lib64/php/modules/
@@ -82,4 +82,4 @@ RUN chmod +x /app/entrypoint.sh \
EXPOSE 8000
# 设置工作目录
WORKDIR /app
WORKDIR /app

View File

@@ -2,6 +2,7 @@
namespace App\Filament\Pages;
use App\Services\NetworkService;
use App\Settings\NetworkSettings;
use Filament\Forms;
use Filament\Forms\Form;
@@ -19,10 +20,53 @@ class ManageNetwork extends SettingsPage
protected static ?string $navigationLabel = '网络连接配置管理';
protected static ?string $title = '网络连接配置管理';
public function form(Form $form): Form
{
// 获取当前网络接口
$interfaces = NetworkService::getNetworkInterfaces();
return $form
->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 => "",
@@ -33,7 +77,8 @@ class ManageNetwork extends SettingsPage
->required()
->reactive(),
Fieldset::make()
// 动态显示的网络信息字段
Fieldset::make('网络配置')
->schema([
TextInput::make('ip_address')
->label("IP地址")

View File

@@ -0,0 +1,45 @@
<?php
namespace App\Services;
class NetworkService
{
public static function getNetworkInterfaces()
{
// 使用 `ifconfig` 来获取接口和 IP 地址
$output = shell_exec('ifconfig');
return self::parseNetworkInfo($output);
}
private static function parseNetworkInfo($output)
{
$interfaces = [];
$interface = null;
$lines = explode("\n", $output);
foreach ($lines as $line) {
// 匹配接口名称
if (preg_match('/^([a-zA-Z0-9-]+): flags=/', $line, $matches)) {
$interface = $matches[1];
$interfaces[$interface] = [
'ip' => null,
'mask' => null,
];
}
// 匹配 IPv4 地址
if ($interface && preg_match('/inet ([\d\.]+)/', $line, $matches)) {
$interfaces[$interface]['ip'] = $matches[1];
}
// 匹配子网掩码
if ($interface && preg_match('/netmask ([\d\.]+)/', $line, $matches)) {
$interfaces[$interface]['mask'] = $matches[1];
}
}
// 输出调试信息,检查解析到的接口
error_log(print_r($interfaces, true));
return $interfaces;
}
}