python yum check-update howto


If you are running an rpm based Linux system like CentOS you might want to automatically and regularly check if security-updates are available. You can use existing commandline tools or python scripts.

This article is about checking if linux host is up-to-date with python yum module – without scraping the cli tool. The yum commandline tool has a nice option to see if a host is up2date or if there are any (security-)updates available. On the other hand – default python installations offer a “yum” module – which doesn’t seem to have such an easy option.

After analysing the yum-cli code (which is also python) I found out that this option is just not very well documented … see the following code example howto to check for updates with the yum python module:

import yum

base = yum.YumBase()
package_list = base.doPackageLists(pkgnarrow='updates', patterns='', ignore_case=True)

if package_list.updates:
  print 'updates available'
  for pkg in package_list.updates
    print pkg

Sample Run

python rpm-test.py

Loaded plugins: product-id, rhnplugin
This system is receiving updates from RHN Classic or RHN Satellite.

updates available

openssl-1.0.1e-30.el6_6.5.i686
libssh2-1.4.2-1.el6_6.1.i686
libcurl-7.19.7-40.el6_6.4.i686
libXtst-1.2.2-2.1.el6.i686
libXi-1.7.2-2.2.el6.i686
libXext-1.3.2-2.1.el6.i686
libxcb-1.9.1-2.el6.i686
libX11-1.6.0-2.2.el6.i686
krb5-libs-1.10.3-33.el6.i686
keyutils-libs-1.4-5.el6.i686
sqlite-3.6.20-1.el6.i686
openldap-2.4.39-8.el6.i686
nss-softokn-3.14.3-22.el6_6.i686
nss-3.16.2.3-3.el6_6.i686
libXau-1.0.6-4.el6.i686
cyrus-sasl-lib-2.1.23-15.el6_6.1.i686
zlib-1.2.3-29.el6.i686
readline-6.0-4.el6.i686
nss-util-3.16.2.3-2.el6_6.i686
nspr-4.10.6-1.el6_5.i686
ncurses-libs-5.7-3.20090208.el6.i686
libidn-1.18-2.el6.i686
libcom_err-1.41.12-21.el6.i686
tcsh-6.17-25.el6_6.x86_64
nss-softokn-freebl-3.14.3-22.el6_6.i686
mdadm-3.3-6.el6_6.1.x86_64
libXfont-1.4.5-4.el6_6.x86_64
alsa-utils-1.0.22-9.el6_6.x86_64
tzdata-2015a-1.el6.noarch
subscription-manager-1.12.14-9.el6_6.x86_64
kernel-headers-2.6.32-504.8.1.el6.x86_64
jasper-libs-1.900.1-16.el6_6.3.x86_64
busybox-1.15.1-21.el6_6.x86_64
nss-tools-3.16.2.3-3.el6_6.x86_64
mailx-12.4-8.el6_6.x86_64
kpartx-0.4.9-80.el6_6.3.x86_64
cyrus-sasl-plain-2.1.23-15.el6_6.1.x86_64
cyrus-sasl-2.1.23-15.el6_6.1.x86_64
rhnpush-5.5.81-8.el6sat.noarch
rhncfg-actions-5.10.74-7.el6sat.noarch
perl-CGI-3.51-136.el6_6.1.x86_64
openssh-server-5.3p1-104.el6_6.1.x86_64
openssh-clients-5.3p1-104.el6_6.1.x86_64
lvm2-2.02.111-2.el6_6.1.x86_64
bind-utils-9.8.2-0.30.rc1.el6_6.1.x86_64
subversion-1.6.11-12.el6_6.x86_64
perl-Test-Simple-0.92-136.el6_6.1.x86_64
java-1.7.0-oracle-1.7.0.75-1jpp.1.el6.x86_64
[...snip ...]