如果您正在使用类似 Debian 的发行版并且是新用户或刚刚开始作为 系统管理员 的职业生涯,您可能已经知道更新软件包的重要性使用 apt 更新。您可能还想了解每个软件包的用途,以了解有关 Linux 的更多信息。此外,系统管理员 (sysadmin) 经常需要与利益相关者沟通或记录哪些更新是紧急的或与安全相关的。
在这篇文章中,我将向您展示如何结合 Python、apt list -u 命令和 Gemini AI 来创建人类可读的待处理软件包更新摘要。
apt list -u | awk '{ print }' | sed 's|/.*||'
以下是两个脚本的解决方案的细分:
此脚本运行 apt list -u 来获取挂起的更新,处理输出,并使用提示功能从 Gemini AI 获取分类摘要。
import subprocess from utils.gemini_cfg import prompt try: # Run 'apt list -u' to list upgradable packages result = subprocess.run(["apt", "list", "-u"], capture_output=True, text=True, check=True) output = result.stdout # Get command output # Use the Gemini AI model to summarize the updates summary = prompt(output) # Save the AI-generated summary to a Markdown file with open("./gemini_result.md", "w") as file: file.write(summary) print("Summary saved to gemini_result.md") except subprocess.CalledProcessError as e: print("Error while running apt list:", e)
此脚本配置 Gemini API 并定义 AI 生成内容的提示功能。
import google.generativeai as genai from environs import Env # Load API key from .env file env = Env() env.read_env() key = env("TOKEN") # Replace with your environment variable key name # Configure Gemini API genai.configure(api_key=key) model = genai.GenerativeModel("gemini-1.5-flash") # Function to prompt Gemini AI for summaries def prompt(content): message = ( "You work as a sysadmin (Debian server infrastructure). " "You must create a list categorizing the importance in terms of security and priority, " "providing a brief summary for each package so that business managers can understand " "what each library is from this output of the `apt list -u` command: " f"{content}" ) response = model.generate_content([message]) return response.text
该脚本执行以下操作:
打开gemini_result.md 可查看清晰、分类的更新摘要,以便于沟通。
以下是生成的摘要的示例:
## Debian Package Update List: Priority and Security The list below categorizes the packages available for update, considering their importance in terms of security and business operation priority. The classification is subjective and may vary depending on your company's specific context. **Category 1: High Priority - Critical Security (update immediately)** - **linux-generic, linux-headers-generic:** Critical kernel updates to fix security vulnerabilities. - **libcurl4:** Resolves potential security issues for data transfer operations. ... **Category 2: High Priority - Maintenance and Stability (update soon)** * **`e2fsprogs`, `logsave`:** Packages related to ext2/ext3/ext4 file systems. Update to ensure data integrity and file system stability. **Medium-High priority.** ... **Category 3: Medium Priority - Applications (update as needed)** * **`code`:** Visual Studio Code editor. Update for new features and bug fixes, but not critical for system security. * **`firefox`, `firefox-locale-en`, `firefox-locale-pt`:** Firefox browser. Updates for security fixes and new functionalities. Priority depends on Firefox usage in your infrastructure. ...
借助一点 Python 和 Gemini AI,您可以自动化并改进 Debian 软件包更新的沟通方式。该脚本是将 AI 集成到系统管理工作流程中的良好基础。这篇文章出于教育目的,因此请注意 Gemini API 资源以及系统的安全处理。
感谢您的阅读! ?
以上是使用 Python 和 Gemini (gemini--flash) 自动生成 Debian 软件包更新摘要的详细内容。更多信息请关注PHP中文网其他相关文章!