Pyghmi: Python General Hardware Management Initiative
Pyghmi is a pure Python implementation of the Intelligent Platform Management Interface (IPMI) protocol, also supporting other hardware management initiatives. It enables programmatic control over server hardware, including power management, sensor monitoring, and console redirection (SOL). The library is actively maintained as part of OpenStack projects, with the current version 1.6.15 released on April 10, 2026, indicating a regular release cadence.
Common errors
-
Error: Session is logged out
cause The IPMI session was idle for too long and the BMC automatically terminated it.fixImplement a background thread to run `ipmi_cmd.eventloop()` to prevent session timeouts. For example: `import threading; house_keeper_thread = threading.Thread(target=ipmi_cmd.eventloop, daemon=True); house_keeper_thread.start()` -
No module named 'pyghmi.ipmi'
cause The `pyghmi` package is either not installed, or there's a typo in the import statement.fixEnsure `pyghmi` is correctly installed via `pip install pyghmi`. Verify the import statement is `from pyghmi.ipmi import ...`. -
Unable to establish IPMI v2 / RMCP+ session
cause Common causes include incorrect BMC IP, username, password, network firewall blocking UDP port 623, or the BMC not supporting IPMI v2.0/RMCP+.fixVerify all connection parameters (IP, user, pass). Check firewall rules on both client and BMC. Use `ipmitool` from the command line to confirm basic connectivity if possible (e.g., `ipmitool -H <bmc_ip> -U <user> -P <pass> chassis power status`).
Warnings
- gotcha IPMI sessions can time out if idle for too long (e.g., 30 seconds), leading to 'Session is logged out' errors or deadlocks on subsequent commands.
- breaking The library was originally named 'ipmi' and later renamed to 'pyghmi'. Attempting to import or use older 'ipmi' references will result in `ModuleNotFoundError` or similar issues.
- gotcha When trying to establish an IPMI v2 / RMCP+ session, errors can occur due to network issues, incorrect credentials, or unsupported IPMI versions/configurations on the BMC itself.
Install
-
pip install pyghmi
Imports
- command
import pyghmi.ipmi.command
from pyghmi.ipmi import command
- bmc
from pyghmi import bmc
from pyghmi.ipmi import bmc
Quickstart
import os
from pyghmi.ipmi import command
# Environment variables for BMC connection
BMC_IP = os.environ.get('PYGHMI_BMC_IP', '192.168.1.100')
BMC_USER = os.environ.get('PYGHMI_BMC_USER', 'admin')
BMC_PASS = os.environ.get('PYGHMI_BMC_PASSWORD', 'password')
try:
# Establish an IPMI command session
# The `command.Command` object automatically manages session lifecycle.
ipmi_cmd = command.Command(
bmc=BMC_IP,
userid=BMC_USER,
password=BMC_PASS
)
print(f"Connected to BMC at {BMC_IP}...")
# Get power state
power_state = ipmi_cmd.get_power_state()
print(f"Power State: {power_state['powerstate']}")
except Exception as e:
print(f"An error occurred: {e}")
print("Ensure PYGHMI_BMC_IP, PYGHMI_BMC_USER, and PYGHMI_BMC_PASSWORD environment variables are set correctly.")
print("Also verify network connectivity to the BMC and correct IPMI port (default 623).")