使用 Python 和 Gemini (gemini--flash) 自动生成 Debian 软件包更新摘要
如果您正在使用类似 Debian 的发行版并且是新用户或刚刚开始作为 系统管理员 的职业生涯,您可能已经知道更新软件包的重要性使用 apt 更新。您可能还想了解每个软件包的用途,以了解有关 Linux 的更多信息。此外,系统管理员 (sysadmin) 经常需要与利益相关者沟通或记录哪些更新是紧急的或与安全相关的。
在这篇文章中,我将向您展示如何结合 Python、apt list -u 命令和 Gemini AI 来创建人类可读的待处理软件包更新摘要。
目标?
- 使用 apt list -u 命令检索 Debian 上待处理更新的列表。 注意: 如果需要,您可以使用以下命令修改输出:
apt list -u | awk '{ print }' | sed 's|/.*||'
- 将此列表发送给 Gemini AI(使用 Google 的生成库)。
- 使用AI对每个包更新的重要性进行分类和总结。
- 将结果保存为 Markdown 文件以便于分享。
要求 ?
- Python 3.8
- Google Gemini API 密钥
- 所需库:pip install google-generativeai 环境
- 基于 Debian 的系统:此脚本依赖于 apt 命令。
守则
以下是两个脚本的解决方案的细分:
apt_list.py
此脚本运行 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_cfg.py
此脚本配置 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
- 运行 apt_list.py 脚本:python apt_list.py
-
该脚本执行以下操作:
- 检索待处理的 Debian 软件包更新。
- 将列表传递给 Gemini AI 进行分类和解释。
- 将 AI 生成的输出保存到 gemini_result.md。
打开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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

攻克Investing.com的反爬虫策略许多人尝试爬取Investing.com(https://cn.investing.com/news/latest-news)的新闻数据时,常常�...

Python3.6环境下加载pickle文件报错:ModuleNotFoundError:Nomodulenamed...

使用Scapy爬虫时管道文件无法写入的原因探讨在学习和使用Scapy爬虫进行数据持久化存储时,可能会遇到管道文�...
