python 检查CentOS中的可用软件包更新

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 检查CentOS中的可用软件包更新相关的知识,希望对你有一定的参考价值。

# Run in Docker
# docker run -v /home/martin/temp/:/var/scripts -i -t centos:latest python /var/scripts/packagecentos.py

import subprocess
import re



installed_packages_dict = {}
installed_packages = subprocess.Popen(["yum",'--color=never','list', 'installed'], stdout=subprocess.PIPE, close_fds=True,
						).communicate()[0]


for index, line in enumerate(installed_packages.splitlines()):
	regex = re.compile('^(.*?)\s+(.*?)\s+@(.*)$')
	match = regex.match(line)
	if match:
		name, version, repo = match.groups()
		installed_packages_dict[name] = version



packages_for_upgrade_list = []
packages_for_upgrade = subprocess.Popen(["yum",'check-update'], stdout=subprocess.PIPE, close_fds=True,
						).communicate()[0]


for index, line in enumerate(packages_for_upgrade.splitlines()):
	regex = re.compile('^(\S+\.\S+)\s+(\S+)\s+\S+$')
	match = regex.match(line)
	if match:
		name, new_version = match.groups()

		# Check if this version is new 
		currently_installed_version = installed_packages_dict.get(name)
		if currently_installed_version != None:

			if currently_installed_version != new_version:
				
				package_dict = {'name': name, 
					'current_version': currently_installed_version,
					'new_version': new_version
				}
				
				packages_for_upgrade_list.append(package_dict)
		

print packages_for_upgrade_list

以上是关于python 检查CentOS中的可用软件包更新的主要内容,如果未能解决你的问题,请参考以下文章

CentOS7--使用yum安装和管理软件

linux安装源一直显示感叹号

在 CentOS 或 RHEL 系统上检查可用的安全更新的方法

CentOS 6.0系列更新安全补丁

centos7怎样用命令卸载

检查适用于 iOS 的可用软件更新