When working in Linux environments, package management becomes an essential task. A Python script to check installed RPMs provides administrators with a simple yet powerful way to verify packages. In this blog, we will explain the script, how it works, and why it can save valuable time. Moreover, we will highlight its automation benefits, making it easier to manage multiple servers.
The script imports the subprocess module, which is responsible for running shell commands. Subsequently, it executes rpm -qa to list all installed packages. After that, it checks each package from a predefined list and prints whether the package is installed or not.
Here is the mentioned Python script:
#!/usr/bin/python3.11
#
# Check installed RPMs
#
import subprocess
pkg_list = [
"package1",
"package2",
"package3",
"package4",
"package5",
"package6",
]
try:
result = subprocess.run(
["rpm", "-qa"],
stdout=subprocess.PIPE,
text=True,
check=True
)
installed_list = result.stdout.splitlines()
for pkg in pkg_list:
installed_packages = [
line for line in installed_list
if line.startswith(pkg + "-") or line == pkg
]
if installed_packages:
for installed in installed_packages:
print(f"{installed} is installed")
else:
print(f"{pkg} is not installed")
except subprocess.CalledProcessError as e:
print(f"Error running rpm -qa: {e}") Automation: Instead of manually searching through long lists, this Python script to check installed RPMs automates the process.
Clarity: The script prints results in a clear format, making it easy to track missing packages.
Flexibility: By editing the pkg_list, any administrator can adapt it to different requirements.
In conclusion, using a Python script to check installed RPMs offers system administrators an efficient way to manage Linux packages. Since it is simple, flexible, and effective, this approach reduces manual effort. Moreover, by automating checks, you can ensure consistency across multiple systems.
If you are managing several servers or simply want a smarter approach, try using this Python script today. It might just save you time while making package verification much easier.
Keep exploring. Continue reading on our blog.
I enjoyed reading this article. Thanks for sharing your insights.