如果您正在使用類似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中文網其他相關文章!