Skip to content

Instantly share code, notes, and snippets.

@srkiNZ84
Last active March 11, 2022 04:48
Show Gist options
  • Save srkiNZ84/57f22d256be87f59b7575a5584239ef8 to your computer and use it in GitHub Desktop.
Save srkiNZ84/57f22d256be87f59b7575a5584239ef8 to your computer and use it in GitHub Desktop.
Python script to get list of installed packages that have upgrades and output in JSON
#!/usr/bin/python3
# NOTE: Requires the python3-apt package
import apt
import apt_pkg
import json
need_upgrade = []
cache = apt.apt_pkg.Cache()
dep_cache = apt.apt_pkg.DepCache(cache)
for pkg in cache.packages:
if dep_cache.is_upgradable(pkg) and pkg.current_state == apt_pkg.CURSTATE_INSTALLED:
#print(" ", pkg.name)
need_upgrade.append(pkg.name)
print(pkg.name, " needs upgrade. Current version is: ", pkg.current_ver.ver_str, "\n latest is: ", \
dep_cache.get_candidate_ver(pkg).ver_str)
print(json.dumps(need_upgrade))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment