admin 发表于 2026-10-14 09:37:48

在当今数字化信息时代,网络安全与系统性能的重要性日益凸显。Linux 操作系统以其开源、稳定、高效等诸多优势,成为众多服务器和网络设备的首选操作系统。然而,新部署的 Linux 系统往往默认开启了许多不必要的端口、服务和配置信息,这不仅会消耗大量系统资源,还会增加系统遭受攻击的风险,影响系统在高并发场景下的性能表现。<br><br>为了让 Linux 系统在保障安全的同时,能高效稳定地运行,我们可以借助自动化脚本与先进的豆包大模型,构建一套智能化的系统优化方案。下面为你详细介绍该方案的具体操作流程:<br><br>我们要编写一个自动化脚本。这个脚本的主要任务是与豆包大模型的 API 接口建立连接。在连接之前,脚本会收集当前 Linux 系统的详细信息,比如系统的版本、已安装的软件包、正在运行的服务、开放的端口等。这些信息就像是系统的 “健康档案”,能让豆包大模型全面了解系统的现状。<br><br>接着,脚本将收集到的系统信息通过 API 接口发送给豆包大模型。豆包大模型拥有强大的数据分析和决策能力,它就像一位经验丰富的系统专家,能够对这些信息进行深入分析。大模型会综合考虑系统的安全性、性能优化以及高并发场景下的运行需求,判断出哪些端口、服务和配置信息是不必要的,并给出具体的优化建议。<br><br>豆包大模型会将这些优化建议通过 API 接口返回给脚本。这些建议可能包括关闭某些不必要的服务、禁用一些默认开启但当前系统不需要的端口,以及修改一些不合理的配置参数等。<br><br>脚本会根据豆包大模型返回的建议,自动对 Linux 系统进行优化和初始化操作。对于不必要的服务,脚本会使用相应的命令将其停止并禁止开机自启,例如对于 CentOS 系统,可以使用systemctl stop和systemctl disable命令。对于不需要开放的端口,脚本会通过防火墙规则进行关闭,像使用iptables或firewalld来限制端口的访问。同时,脚本还会自动修改系统配置文件,移除或调整那些可能影响系统性能和安全的配置信息。<br><br>脚本会对优化后的系统进行简单的检查,确保各项操作都已正确执行,系统没有出现异常。并且,为了保证系统的稳定性和性能,脚本还会提供操作记录和日志,方便后续的查看和问题排查。<br><br>通过这样一套自动化的优化方案,我们可以充分利用豆包大模型的智能分析能力,快速、准确地找出 Linux 系统中存在的潜在问题,并及时进行优化和初始化。这不仅能有效提升系统的安全性,降低被攻击的风险,还能显著提高系统在高并发场景下的性能表现,为用户提供更加稳定、高效的服务。同时,这种自动化的方式大大减少了人工操作的繁琐和失误,提高了系统管理的效率和准确性。<br><br>此处将使用 Python 3 编写,因此需要确保你的服务器上已经安装了 Python 3。可以使用以下命令检查 Python 版本:<br><br>python3 --version<br><br><img src="https://bbs.jfedu.net/jf_imgs/image1836.png" alt="" style="max-width:100%;"><br><br>安装Python requests 库用于发送 HTTP 请求,是脚本中与模型 API 进行交互的关键库。可以使用以下命令安装:<br><br>mkdir -p ~/.pip &amp;&amp; echo -e &quot;\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple\n\ntrusted-host = pypi.tuna.tsinghua.edu.cn&quot; &gt; ~/.pip/pip.conf<br><br><img src="https://bbs.jfedu.net/jf_imgs/image1837.png" alt="" style="max-width:100%;"><br><br>pip install requestspip install psutil<br><br><img src="https://bbs.jfedu.net/jf_imgs/image1838.png" alt="" style="max-width:100%;"><br><br>编写Python脚本代码,如下所示:<br><br>#!/usr/bin/env python3<br><br># -*- coding: utf-8 -*-<br><br>import requests<br><br>import os<br><br>import json<br><br>import time<br><br>import threading<br><br>import sys<br><br>import subprocess<br><br>import re<br><br>import signal<br><br># ===================== 七牛大模型配置 =====================<br><br>API_KEY = &quot;sk-aab9187dbe9990b5bb92ee6c2945a5619d0f06b00831135d083a0a0a0e50005e&quot;<br><br>API_URL = &#x27;https://api.qnaigc.com/v1/chat/completions&#x27;<br><br>MODEL_NAME = &quot;qwen-turbo&quot;<br><br># =========================================================<br><br># 全局变量,用于控制进度提示线程<br><br>stop_progress = False<br><br># 定义需要保留的服务和端口<br><br>ALLOWED_SERVICES = [&#x27;ssh&#x27;, &#x27;nginx&#x27;, &#x27;dbus&#x27;, &#x27;systemd-journald&#x27;, &#x27;polkit&#x27;, &#x27;NetworkManager&#x27;, &#x27;systemd-hostnamed&#x27;, &#x27;systemd-logind&#x27;, &#x27;systemd-udevd&#x27;]<br><br>ALLOWED_PORTS = <br><br># 定义无效的服务名<br><br>INVALID_SERVICES = [&#x27;UNIT&#x27;, &#x27;LOAD&#x27;, &#x27;ACTIVE&#x27;, &#x27;SUB&#x27;]<br><br># 定义拒绝手动停止的服务<br><br>REFUSE_MANUAL_STOP_SERVICES = [&#x27;auditd&#x27;]<br><br># 定义要关闭的特定服务<br><br>SERVICES_TO_CLOSE = [&#x27;dhcpd&#x27;, &#x27;nfs&#x27;, &#x27;bind&#x27;, &#x27;vsftpd&#x27;, &#x27;named&#x27;]<br><br># 定义特定服务对应的端口<br><br>SERVICE_PORT_MAPPING = {<br><br>&#x27;dhcpd&#x27;: ,<br><br>&#x27;nfs&#x27;: ,<br><br>&#x27;bind&#x27;: ,<br><br>&#x27;vsftpd&#x27;: ,<br><br>&#x27;named&#x27;: <br><br>}<br><br>def handle_ctrl_c(signum, frame):<br><br>print(&quot;\n\n🛑 检测到 Ctrl+C,程序正常退出!&quot;)<br><br>sys.exit(0)<br><br>signal.signal(signal.SIGINT, handle_ctrl_c)<br><br># 新增:检查系统命令是否存在<br><br>def check_command_exists(cmd):<br><br>&quot;&quot;&quot;检查系统命令是否存在,避免执行不存在的命令报错&quot;&quot;&quot;<br><br>try:<br><br>subprocess.run(, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)<br><br>return True<br><br>except FileNotFoundError:<br><br>print(f&quot;\033, []<br><br>def get_running_services():<br><br>&quot;&quot;&quot;获取当前正在运行的服务&quot;&quot;&quot;<br><br>if not check_command_exists(&#x27;systemctl&#x27;):<br><br>return []<br><br>try:<br><br>result = subprocess.run(<br><br>[&#x27;systemctl&#x27;, &#x27;list-units&#x27;, &#x27;--type=service&#x27;, &#x27;--state=running&#x27;, &#x27;--no-pager&#x27;, &#x27;--plain&#x27;],<br><br>capture_output=True, text=True, timeout=10<br><br>)<br><br>output = result.stdout<br><br>services = []<br><br>for line in output.splitlines():<br><br>parts = line.split()<br><br>if len(parts) &gt; 0:<br><br>service = parts.replace(&#x27;.service&#x27;, &#x27;&#x27;)<br><br>if service not in INVALID_SERVICES and service.strip():<br><br>services.append(service)<br><br>return list(set(services))<br><br>except subprocess.TimeoutExpired:<br><br>print(&quot;获取运行中服务超时&quot;)<br><br>return []<br><br>except Exception as e:<br><br>print(f&quot;获取运行中服务失败: {e}&quot;)<br><br>return []<br><br>def get_listening_ports():<br><br>&quot;&quot;&quot;获取当前正在监听的端口&quot;&quot;&quot;<br><br>if not check_command_exists(&#x27;ss&#x27;):<br><br>return []<br><br>try:<br><br>result = subprocess.run(<br><br>[&#x27;ss&#x27;, &#x27;-tuln&#x27;, &#x27;--no-header&#x27;],<br><br>capture_output=True, text=True, timeout=10<br><br>)<br><br>output = result.stdout<br><br>ports = []<br><br>for line in output.splitlines():<br><br>parts = line.split()<br><br>if len(parts) &gt; 4:<br><br>address_port = parts.split(&#x27;:&#x27;)<br><br>if len(address_port) &gt; 1:<br><br>try:<br><br>port = int(address_port[-1])<br><br>ports.append(port)<br><br>except ValueError:<br><br>pass<br><br>return list(set(ports))<br><br>except subprocess.TimeoutExpired:<br><br>print(&quot;获取监听端口超时&quot;)<br><br>return []<br><br>except Exception as e:<br><br>print(f&quot;获取监听端口失败: {e}&quot;)<br><br>return []<br><br>def progress_indicator():<br><br>&quot;&quot;&quot;进度转圈提示&quot;&quot;&quot;<br><br>global stop_progress<br><br>symbols = [&#x27;-&#x27;, &#x27;\\&#x27;, &#x27;|&#x27;, &#x27;/&#x27;]<br><br>index = 0<br><br>while not stop_progress:<br><br>symbol = symbols<br><br>sys.stdout.write(f&quot;\r正在请求大模型分析无用服务与端口,请耐心等待... {symbol}&quot;)<br><br>sys.stdout.flush()<br><br>time.sleep(0.2)<br><br>index += 1<br><br>sys.stdout.write(&quot;\r请求完成,已接收AI优化建议\n&quot;)<br><br>def get_optimized_suggestions(services, ports):<br><br>&quot;&quot;&quot;调用AI获取关闭不必要服务和端口的建议&quot;&quot;&quot;<br><br>headers = {<br><br>&#x27;Authorization&#x27;: f&#x27;Bearer {API_KEY}&#x27;,<br><br>&#x27;Content-Type&#x27;: &#x27;application/json&#x27;<br><br>}<br><br>prompt = f&quot;&quot;&quot;当前系统正在运行的服务有:{&#x27;, &#x27;.join(services)},正在监听的端口有:{&#x27;, &#x27;.join(map(str, ports))}。<br><br>要求:<br><br>1. 仅返回可以关闭的不必要服务名和端口号,英文逗号分隔;<br><br>2. 必须排除保留服务:{&#x27;, &#x27;.join(ALLOWED_SERVICES)} 和保留端口:{&#x27;, &#x27;.join(map(str, ALLOWED_PORTS))};<br><br>3. 只返回纯列表,不要任何解释、标题、注释;<br><br>4. 服务名不带.service后缀,端口仅输出数字。<br><br>示例输出:dhcpd,nfs,67,68&quot;&quot;&quot;<br><br>data = {<br><br>&quot;model&quot;: MODEL_NAME,<br><br>&quot;messages&quot;: [<br><br>{&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;Linux安全运维专家,严格遵守输出格式,只返回逗号分割的服务与端口列表。&quot;},<br><br>{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: prompt}<br><br>]<br><br>}<br><br>try:<br><br>print(&quot;正在向大模型请求无用服务/端口清理建议...&quot;)<br><br>global stop_progress<br><br>stop_progress = False<br><br>progress_thread = threading.Thread(target=progress_indicator)<br><br>progress_thread.start()<br><br>response = requests.post(API_URL, headers=headers, json=data, timeout=120)<br><br>stop_progress = True<br><br>progress_thread.join()<br><br>response.raise_for_status()<br><br>response_data = response.json()<br><br>content = response_data[&quot;choices&quot;][&quot;message&quot;][&quot;content&quot;]<br><br># 剥离代码块标记<br><br>block_reg = re.compile(r&#x27;```(?:txt|shell)?(.*?)```&#x27;, re.DOTALL)<br><br>match = block_reg.search(content)<br><br>if match:<br><br>return match.group(1).strip()<br><br>return content.strip()<br><br>except requests.RequestException as e:<br><br>stop_progress = True<br><br>progress_thread.join()<br><br>print(f&quot;\033<br><br>ports_to_close = []<br><br>if not suggestions:<br><br>return services_to_stop, ports_to_close<br><br>items = suggestions.split(&#x27;,&#x27;)<br><br>for item in items:<br><br>item = item.strip().replace(&#x27; &#x27;, &#x27;&#x27;)<br><br>if not item:<br><br>continue<br><br>if re.match(r&#x27;^\d+$&#x27;, item):<br><br>try:<br><br>port = int(item)<br><br>if port not in ALLOWED_PORTS and 1 &lt;= port &lt;= 65535:<br><br>ports_to_close.append(port)<br><br>except ValueError:<br><br>pass<br><br>elif item not in INVALID_SERVICES and item not in ALLOWED_SERVICES and item not in REFUSE_MANUAL_STOP_SERVICES:<br><br>services_to_stop.append(item)<br><br>return list(set(services_to_stop)), list(set(ports_to_close))<br><br>def stop_unnecessary_services(services_to_stop):<br><br>&quot;&quot;&quot;停止不必要的服务&quot;&quot;&quot;<br><br>if not check_command_exists(&#x27;systemctl&#x27;):<br><br>return [], []<br><br>stopped_services = []<br><br>skipped_services = []<br><br>devnull = open(os.devnull, &#x27;w&#x27;)<br><br>print(f&quot;\n准备停止以下服务: {&#x27;, &#x27;.join(services_to_stop)}&quot;)<br><br>for service in services_to_stop:<br><br>if service in ALLOWED_SERVICES:<br><br>print(f&quot;跳过保留服务: {service}&quot;)<br><br>continue<br><br>if service in REFUSE_MANUAL_STOP_SERVICES:<br><br>print(f&quot;拒绝停止受保护服务: {service}&quot;)<br><br>skipped_services.append(service)<br><br>continue<br><br>try:<br><br>check = subprocess.run(<br><br>[&#x27;systemctl&#x27;, &#x27;is-active&#x27;, f&#x27;{service}.service&#x27;],<br><br>capture_output=True, text=True, timeout=5<br><br>)<br><br>if check.returncode != 0:<br><br>continue<br><br>subprocess.run(<br><br>[&#x27;systemctl&#x27;, &#x27;stop&#x27;, f&#x27;{service}.service&#x27;],<br><br>stdout=devnull, stderr=devnull, timeout=10<br><br>)<br><br>subprocess.run(<br><br>[&#x27;systemctl&#x27;, &#x27;disable&#x27;, f&#x27;{service}.service&#x27;],<br><br>stdout=devnull, stderr=devnull, timeout=10<br><br>)<br><br>stopped_services.append(service)<br><br>print(f&quot;✅ 成功停止并禁用服务: {service}&quot;)<br><br>except subprocess.TimeoutExpired:<br><br>print(f&quot;⏱️ 停止服务 {service} 超时&quot;)<br><br>except Exception as e:<br><br>print(f&quot;❌ 停止服务 {service} 失败: {e}&quot;)<br><br>devnull.close()<br><br>return stopped_services, skipped_services<br><br>def check_firewalld_status():<br><br>&quot;&quot;&quot;检查 firewalld 是否运行&quot;&quot;&quot;<br><br>if not check_command_exists(&#x27;firewall-cmd&#x27;):<br><br>return False<br><br>try:<br><br>result = subprocess.run(<br><br>[&#x27;systemctl&#x27;, &#x27;is-active&#x27;, &#x27;firewalld&#x27;],<br><br>capture_output=True, text=True, timeout=5<br><br>)<br><br>return result.stdout.strip() == &#x27;active&#x27;<br><br>except Exception as e:<br><br>print(f&quot;检查firewalld状态失败: {e}&quot;)<br><br>return False<br><br>def close_unnecessary_ports(ports_to_close, services_to_stop):<br><br>&quot;&quot;&quot;防火墙关闭多余端口&quot;&quot;&quot;<br><br>if not check_command_exists(&#x27;firewall-cmd&#x27;):<br><br>print(&quot;缺少firewall-cmd命令,无法操作防火墙端口&quot;)<br><br>return []<br><br>closed_ports = []<br><br>if &#x27;firewalld&#x27; in services_to_stop:<br><br>print(&quot;firewalld即将停止,跳过端口操作&quot;)<br><br>return closed_ports<br><br>if not check_firewalld_status():<br><br>devnull = open(os.devnull, &#x27;w&#x27;)<br><br>print(&quot;firewalld未启动,进行启动与开机自启配置&quot;)<br><br>subprocess.run([&#x27;systemctl&#x27;, &#x27;start&#x27;, &#x27;firewalld&#x27;], stdout=devnull, stderr=devnull, timeout=10)<br><br>subprocess.run([&#x27;systemctl&#x27;, &#x27;enable&#x27;, &#x27;firewalld&#x27;], stdout=devnull, stderr=devnull, timeout=10)<br><br>devnull.close()<br><br>devnull = open(os.devnull, &#x27;w&#x27;)<br><br>print(f&quot;\n准备防火墙关闭端口: {&#x27;, &#x27;.join(map(str, ports_to_close))}&quot;)<br><br>for port in ports_to_close:<br><br>if port in ALLOWED_PORTS:<br><br>print(f&quot;跳过保留端口: {port}&quot;)<br><br>continue<br><br>try:<br><br>subprocess.run([&#x27;firewall-cmd&#x27;, &#x27;--permanent&#x27;, &#x27;--remove-port&#x27;, f&#x27;{port}/tcp&#x27;], stdout=devnull, stderr=devnull)<br><br>subprocess.run([&#x27;firewall-cmd&#x27;, &#x27;--permanent&#x27;, &#x27;--remove-port&#x27;, f&#x27;{port}/udp&#x27;], stdout=devnull, stderr=devnull)<br><br>subprocess.run([&#x27;firewall-cmd&#x27;, &#x27;--reload&#x27;], stdout=devnull, stderr=devnull)<br><br>closed_ports.append(port)<br><br>print(f&quot;✅ 防火墙移除端口: {port}&quot;)<br><br>except Exception as e:<br><br>print(f&quot;❌ 关闭端口 {port} 失败: {e}&quot;)<br><br>devnull.close()<br><br>return closed_ports<br><br>def close_specific_services_and_ports():<br><br>&quot;&quot;&quot;强制清理预设危险服务&quot;&quot;&quot;<br><br>running_services = get_running_services()<br><br>stopped_services = []<br><br>closed_ports = []<br><br>devnull = open(os.devnull, &#x27;w&#x27;)<br><br>print(f&quot;\n处理预设待关闭服务列表: {&#x27;, &#x27;.join(SERVICES_TO_CLOSE)}&quot;)<br><br>for service in SERVICES_TO_CLOSE:<br><br>if service not in running_services:<br><br>print(f&quot;服务 {service} 未运行,跳过&quot;)<br><br>continue<br><br>try:<br><br>subprocess.run([&#x27;systemctl&#x27;, &#x27;stop&#x27;, f&#x27;{service}.service&#x27;], stdout=devnull, stderr=devnull, timeout=10)<br><br>subprocess.run([&#x27;systemctl&#x27;, &#x27;disable&#x27;, f&#x27;{service}.service&#x27;], stdout=devnull, stderr=devnull, timeout=10)<br><br>stopped_services.append(service)<br><br>print(f&quot;✅ 成功停止预设服务: {service}&quot;)<br><br>except Exception as e:<br><br>print(f&quot;❌ 停止服务 {service} 失败: {e}&quot;)<br><br>if service in SERVICE_PORT_MAPPING:<br><br>for port in SERVICE_PORT_MAPPING:<br><br>if port in ALLOWED_PORTS:<br><br>continue<br><br>try:<br><br>subprocess.run([&#x27;firewall-cmd&#x27;, &#x27;--permanent&#x27;, &#x27;--remove-port&#x27;, f&#x27;{port}/tcp&#x27;], stdout=devnull, stderr=devnull)<br><br>subprocess.run([&#x27;firewall-cmd&#x27;, &#x27;--permanent&#x27;, &#x27;--remove-port&#x27;, f&#x27;{port}/udp&#x27;], stdout=devnull, stderr=devnull)<br><br>subprocess.run([&#x27;firewall-cmd&#x27;, &#x27;--reload&#x27;], stdout=devnull, stderr=devnull)<br><br>closed_ports.append(port)<br><br>print(f&quot;✅ 防火墙移除预设端口: {port}&quot;)<br><br>except Exception as e:<br><br>print(f&quot;❌ 端口 {port} 操作失败: {e}&quot;)<br><br>devnull.close()<br><br>return stopped_services, closed_ports<br><br>def summarize_results(<br><br>api_suggestions,<br><br>api_stopped_services,<br><br>api_skipped_services,<br><br>api_closed_ports,<br><br>specific_stopped_services,<br><br>specific_closed_ports<br><br>):<br><br>&quot;&quot;&quot;输出执行汇总信息&quot;&quot;&quot;<br><br>filtered_suggestions = []<br><br>if api_suggestions:<br><br>services_to_stop, ports_to_close = parse_suggestions(api_suggestions)<br><br>filtered_suggestions.extend(services_to_stop)<br><br>filtered_suggestions.extend(map(str, ports_to_close))<br><br>print(&quot;\n&quot; + &quot;=&quot;*60)<br><br>print(&quot;📋 系统服务与端口清理 执行结果汇总&quot;)<br><br>print(&quot;=&quot;*60)<br><br>print(&quot;1. AI分析建议(过滤后):&quot;)<br><br>if filtered_suggestions:<br><br>print(f&quot;   → {&#x27;, &#x27;.join(filtered_suggestions)}&quot;)<br><br>else:<br><br>print(&quot;   → 无有效清理建议&quot;)<br><br>print(&quot;\n2. 根据AI建议执行结果:&quot;)<br><br>if api_stopped_services:<br><br>print(f&quot;   ✅ 已停止服务:{&#x27;, &#x27;.join(api_stopped_services)}&quot;)<br><br>else:<br><br>print(&quot;   - 未停止任何服务&quot;)<br><br>if api_skipped_services:<br><br>print(f&quot;   ⚠️受保护跳过服务:{&#x27;, &#x27;.join(api_skipped_services)}&quot;)<br><br>if api_closed_ports:<br><br>print(f&quot;   ✅ 防火墙关闭端口:{&#x27;, &#x27;.join(map(str, api_closed_ports))}&quot;)<br><br>else:<br><br>print(&quot;   - 未关闭任何端口&quot;)<br><br>print(&quot;\n3. 预设强制清理列表执行结果:&quot;)<br><br>if specific_stopped_services:<br><br>print(f&quot;   ✅ 已停止服务:{&#x27;, &#x27;.join(specific_stopped_services)}&quot;)<br><br>else:<br><br>print(&quot;   - 无匹配运行服务&quot;)<br><br>if specific_closed_ports:<br><br>print(f&quot;   ✅ 防火墙关闭端口:{&#x27;, &#x27;.join(map(str, specific_closed_ports))}&quot;)<br><br>else:<br><br>print(&quot;   - 无端口变更&quot;)<br><br>print(&quot;=&quot;*60)<br><br>def main():<br><br>print(&quot;================ Linux系统无用服务&amp;端口安全清理工具 ================&quot;)<br><br>check_command_exists(&#x27;systemctl&#x27;)<br><br>check_command_exists(&#x27;ss&#x27;)<br><br>check_command_exists(&#x27;firewall-cmd&#x27;)<br><br>try:<br><br>services, ports = get_current_services_and_ports()<br><br>print(f&quot;\n当前运行服务: {&#x27;, &#x27;.join(services)}&quot;)<br><br>print(f&quot;当前监听端口: {&#x27;, &#x27;.join(map(str, ports))}&quot;)<br><br>suggestions = get_optimized_suggestions(services, ports)<br><br>api_stopped_services = []<br><br>api_skipped_services = []<br><br>api_closed_ports = []<br><br>specific_stopped_services = []<br><br>specific_closed_ports = []<br><br>if suggestions:<br><br>services_to_stop, ports_to_close = parse_suggestions(suggestions)<br><br>print(f&quot;\nAI解析待处理服务:{&#x27;, &#x27;.join(services_to_stop)}&quot;)<br><br>print(f&quot;AI解析待处理端口:{&#x27;, &#x27;.join(map(str, ports_to_close))}&quot;)<br><br>api_stopped_services, api_skipped_services = stop_unnecessary_services(services_to_stop)<br><br>api_closed_ports = close_unnecessary_ports(ports_to_close, services_to_stop)<br><br>specific_stopped_services, specific_closed_ports = close_specific_services_and_ports()<br><br>summarize_results(suggestions, api_stopped_services, api_skipped_services, api_closed_ports,<br><br>specific_stopped_services, specific_closed_ports)<br><br>except Exception as e:<br><br>print(f&quot;\033[31m脚本执行异常: {e}\033[0m&quot;)<br><br>sys.exit(1)<br><br>print(&quot;\n🏁 全部任务执行完成!&quot;)<br><br>if __name__ == &quot;__main__&quot;:<br><br>main()<br><br>使用豆包大模型需要登录火山引擎平台去创建应用、获取API秘钥,操作的方法和步骤如下:<br><br>#注册并登录火山引擎平台<br><br>访问火山引擎官网,点击右上角 “注册” 按钮,按照提示完成注册流程。注册方式可以选择手机号、邮箱等,完成注册后登录平台。<br><br>#开通大模型服务<br><br>登录后,在控制台页面,通过搜索框搜索 “大模型”,或者在产品与服务列表中找到 “大模型” 服务并进入。<br><br>若你是首次使用该服务,系统会提示你开通,按照引导完成开通操作。开通过程中可能需要进行实名认证等步骤,请根据系统提示完成。<br><br>#创建应用<br><br>进入大模型服务控制台后,通常在左侧导航栏中找到 “应用管理” 选项并点击进入。<br><br>#在应用管理页面,点击 “创建应用” 按钮。<br><br>填写应用信息:<br><br>应用名称:为你的应用取一个有意义的名称,方便识别和管理,例如 “Nginx 配置建议应用”。<br><br>#应用描述:简要描述该应用的用途,如 “用于根据 Nginx 并发连接数获取 worker_processes 建议值”。<br><br>选择模型:根据你的需求选择合适的大模型,例如可以选择豆包模型。<br><br>填写完成后,点击 “确定” 按钮,系统会创建一个新的应用。<br><br>#获取 API Key 和 Secret Key<br><br>应用创建成功后,在应用管理列表中找到你刚创建的应用,点击该应用名称进入应用详情页。<br><br>在应用详情页中,你可以看到 “API Key” 和 “Secret Key”,这两个密钥用于调用大模型 API 时进行身份验证。注意,Secret Key 只会在首次创建应用时显示一次,请妥善保存。<br><br><img src="https://bbs.jfedu.net/jf_imgs/image1840.png" alt="" style="max-width:100%;"><br><br><img src="https://bbs.jfedu.net/jf_imgs/image1841.png" alt="" style="max-width:100%;"><br><br>通过如上Python脚本+doubao大模型,即可实现自动调整Linux内核配置文件参数,脚本测试如图所示:python3 doubao_init_system.sh<br><br><img src="https://bbs.jfedu.net/jf_imgs/image1848.png" alt="" style="max-width:100%;"><br><br><img src="https://bbs.jfedu.net/jf_imgs/image1849.png" alt="" style="max-width:100%;">
页: [1]
查看完整版本: 大模型初始化Linux系统