有没有办法让 python 显示服务器上安装的所有 apt/yum 包?我有一个程序只能抓取我指定的一个包,但我想知道是否有 apt-show-versions/yum check-update 像 python 中的模块,因为 python-yum 和 python-apt 只做单个包.
谢谢。
编辑:
这是我目前的代码:
# For finding the package version and using the package name -i
def aptpkg(package_name):
cache = apt.Cache()
pkg = cache[package_name]
host = subprocess.Popen('hostname', stdout=subprocess.PIPE, universal_newlines=True).stdout.read().strip()
if pkg.is_installed:
print host
print 'Current ' + package_name + ' installed:', pkg.installed.version
con.execute("insert into ansible_packagelist(date, host, package_name, installed_version) values (current_timestamp,%s,%s,%s)", (host, package_name, pkg.installed.version,))
else:
print host, package_name + ' is not installed on this system.\n'
if pkg.is_upgradable:
print 'Upgradeable version of ' + package_name + ' :', pkg.candidate.version
con.execute("update ansible_packagelist set upgradeable_version = %s where package_name = %s", (pkg.candidate.version, package_name))
db.commit()
最佳答案
这是 pythonic 方式:
import apt
cache = apt.Cache()
for mypkg in cache:
if cache[mypkg.name].is_installed:
print mypkg.name
对于yum,我需要先登录到RH/Centos系统。 但是您可以查看 yum 库,您会发现类似“rpmdb.searchNevra”的内容
关于Python 显示所有已安装的包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26105433/