运维系统开发,服务器设备 BMC 接口信息获取
服务器BMC接口查看与开启(运维系统开发视角)
BMC(基板管理控制器,IPMI底层),主流协议:IPMI 2.0;厂商实现:iDRAC(戴尔)、iLO(惠普HPE)、IMM/XClarity(联想)、iBMC(华为)。
运维平台开发一般两种用法:本地操作系统内查询BMC信息、远端直接访问BMC网口。
一、先分清两个概念
- BMC网口:独立管理网口(物理网卡,单独IP,不依赖OS),服务器关机也能访问;
- IPMI通道:BMC内部通信通道(Channel 1/2等),分本地通道、LAN通道(远程访问开关)。
二、操作系统内查看/开启BMC(Linux,运维采集常用)
前提:服务器主板BMC驱动已加载,OS内可访问BMC设备
/dev/ipmi0
工具包:ipmitool(运维系统最常用,开发集成可调用cli或对接ipmi库)
1. 安装 ipmitool
# RHEL/CentOS/Rocky
yum install ipmitool -y
# Debian/Ubuntu
apt install ipmitool -y
加载内核驱动(必须,否则找不到BMC设备)
modprobe ipmi_devintf
modprobe ipmi_si
2. 查看BMC基础信息
# 查看BMC设备信息、版本
ipmitool mc info
# 查看BMC网口IP、MAC、网关(重点!运维平台采集这个)
ipmitool lan print
# 查看BMC支持的通道列表
ipmitool channel info
lan print 输出关键字段:IP Address, MAC Address, Subnet Mask, Default Gateway。
3. 开启/关闭BMC远程访问(LAN通道)
注意:修改BMC网络必须谨慎,改错会失联!优先本地控制台操作
# 开启LAN通道(启用IPMI over LAN,远程管理开关)
ipmitool lan set 1 access on
# 设置BMC静态IP
ipmitool lan set 1 ipaddr 192.168.0.120
ipmitool lan set 1 netmask 255.255.255.0
ipmitool lan set 1 defgw ipaddr 192.168.0.1
# 设置BMC管理员密码(IPMI账号密码)
ipmitool user set password 2 "Admin@123"
# 启用用户ID2(默认管理员)
ipmitool user enable 2
Channel ID:绝大多数服务器BMC LAN通道是 1;部分华为iBMC是通道2,
ipmitool channel info确认。
4. 验证远程IPMI连通
# 远端机器测试,-I lanplus 使用IPMI2.0
ipmitool -I lanplus -H 192.168.0.120 -U admin -P Admin@123 mc info
三、BIOS层面开启BMC(OS内无法操作时,底层硬件开关)
如果OS里看不到BMC设备,大概率BIOS里BMC控制器/IPMI被禁用。
- 服务器开机,按快捷键进入BIOS:
- 戴尔:F2
- HPE:F9
- 华为:Delete / F11
- 联想:F1
- 找到菜单:
BMC / IPMI / Remote Management - 选项:
- BMC Controller:设置为 Enabled(BMC主控开关)
- IPMI over LAN:Enabled(远程IPMI访问)
- BMC NIC 模式:
- Dedicated:独立BMC网口(推荐,专用管理口)
- Shared:共享业务网卡(业务口复用BMC,很多机型支持)
- 保存重启。
BIOS里也可以直接配置BMC IP/MAC,不需要进操作系统。
四、运维系统开发集成方案(重点)
方案A:调用ipmitool CLI(快速落地,简单运维平台)
- 后端脚本(Python/Go)调用
ipmitool lan print,捕获stdout,正则提取BMC IP、MAC、版本; - 缺点:需要宿主机安装ipmitool+加载ipmi驱动;权限高,需要root;输出文本解析容易受厂商差异影响。
方案B:IPMI 协议SDK(推荐做标准化运维平台)
- Go:
github.com/gebn/ipmi - Python:
pyipmi - 直接走IPMI LANPLUS协议,不需要在被管服务器安装任何agent,平台远端直接连接BMC IP采集信息,这是主流硬件监控平台方案。
方案C:厂商原生REST API(企业级,功能最强)
各厂商BMC自带Redfish API(现在标准),优先Redfish,下一代硬件管理标准,替代IPMI
- Redfish:通用标准,所有新服务器(2018年后)iDRAC/iLO/iBMC都支持RESTful JSON接口
- 运维系统直接HTTP请求BMC地址
/redfish/v1,获取BMC信息、电源、传感器、固件信息。>
✅ 开发建议:新运维平台优先Redfish,IPMI做老旧服务器兼容
示例Redfish查询BMC网络信息:
GET https://<BMC_IP>/redfish/v1/Managers/<BMCManagerId>/EthernetInterfaces
Authorization: Basic base64(admin:password)
返回JSON包含BMC所有网卡IP、MAC、状态。
五、常见坑(开发&运维踩坑点)
- BMC是独立小系统,和业务OSIP无关:BMC IP是单独的管理IP,不要和业务网卡混淆;
- IPMI over LAN 关闭:
lan print里IPMI over LAN Access为 off,远端无法连接; - 防火墙:BMC网口侧防火墙,UDP 623端口(IPMI)要放行;Redfish HTTPS 443;
- 共享网卡模式:业务网卡down掉,BMC网络也会跟着失效;生产环境推荐Dedicated专用口;
- 安全:IPMI明文/弱密码风险;生产建议使用Redfish HTTPS,关闭不必要IPMI账号;
- 部分云服务器/虚拟机:没有BMC硬件,ipmitool无法使用,仅物理机架服务器才有BMC。
六、快速排查清单
ipmitool mc info有没有输出?无输出 → BIOS未开BMC / ipmi驱动没加载;ipmitool lan print看LAN通道是否开启;- 远端
telnet <BMCIP> 623或者 nc 测试UDP 623通不通; - 确认BMC和运维平台网络互通(很多机房BMC单独管理VLAN)。
你可以告诉我你的场景:
是开发运维平台采集BMC资产信息?还是批量自动化开启服务器BMC?我可以给你对应的Python/Go示例代码。
开发运维平台采集BMC资产信息
运维平台开发:采集BMC资产信息完整方案
目标:批量自动采集物理服务器BMC资产(BMC版本、IP/MAC、厂商、SN、硬件型号、电源/风扇/温度传感器)
选型原则:新机器优先 Redfish,老机器兼容 IPMI;两种协议并行,做适配器模式。
架构:平台中心节点发起远程调用,被管服务器不需要安装Agent,只需要BMC网口网络可达。
一、整体架构设计
运维平台后端服务 → Redfish/IPMI Client → BMC管理网口(独立VLAN)
分层:
- 协议适配层:Redfish适配器、IPMI适配器;统一输出标准化资产结构体(屏蔽厂商差异:iDRAC/iLO/iBMC)
- 任务调度层:批量任务、并发控制、超时、重试、失败标记;BMC设备响应普遍慢,并发不宜过高
- 数据存储层:BMC资产表、硬件传感器表、采集任务日志表
- 前端展示:资产清单、BMC在线状态、传感器看板
不推荐:在被管主机部署agent调用本地ipmitool。缺点:需要root、依赖驱动、虚拟机/容器无BMC、部署成本高。
✅ 推荐:远端主动拉取BMC(Out-of-Band带外采集)
二、资产字段设计(数据库表核心字段,BMC资产表)
id 主键
server_sn 服务器整机SN(唯一标识,核心)
bmc_ip BMC管理IP
bmc_mac BMC网卡MAC
bmc_gateway 网关
bmc_netmask 子网掩码
bmc_fw_version BMC固件版本
bmc_model BMC型号(iDRAC9 / iBMC / iLO5)
vendor 服务器厂商:Dell/HPE/Huawei/Inspur
server_model 服务器型号:如 2288H V5、PowerEdge R740
bmc_username 账号(加密存储,禁止明文)
bmc_password 密码(加密存储,AES)
protocol_type 协议类型:redfish / ipmi
status 在线/离线
last_collect_time 上次采集时间
传感器单独一张表:SN关联,传感器名称、类型、当前值、阈值、状态。
三、协议实现方案
方案1:Redfish(首选,标准化REST JSON,推荐新开发)
端口:443 HTTPS,现代服务器标配(Dell iDRAC9+, HPE iLO5+, 华为iBMC V5+)
Redfish 资源路径核心:
/redfish/v1:根服务,获取版本/redfish/v1/Managers:BMC管理器信息(固件版本,BMC网卡)/redfish/v1/Systems:服务器主机信息(SN、机型、CPU、内存)/redfish/v1/Chassis:机箱、电源、风扇、温度传感器
Python 最简Redfish采集示例(获取资产基础信息)
依赖:requests,禁用证书校验(内网BMC大多自签证书)
import requests
import base64
class RedfishClient:
def __init__(self, bmc_ip, username, password):
self.bmc_ip = bmc_ip
self.auth = base64.b64encode(f"{username}:{password}".encode()).decode()
self.headers = {"Authorization": f"Basic {self.auth}"}
self.verify = False
requests.packages.urllib3.disable_warnings()
def get_manager_info(self):
url = f"https://{self.bmc_ip}/redfish/v1/Managers"
resp = requests.get(url, headers=self.headers, verify=self.verify, timeout=10)
data = resp.json()
manager_members = data["Members"]
for member in manager_members:
m_url = member["@odata.id"]
m_resp = requests.get(f"https://{self.bmc_ip}{m_url}", headers=self.headers, verify=self.verify, timeout=10)
m_data = m_resp.json()
fw_version = m_data.get("FirmwareVersion")
# 读取BMC网卡
eth_url = m_data.get("EthernetInterfaces", {}).get("@odata.id")
eth_resp = requests.get(f"https://{self.bmc_ip}{eth_url}", headers=self.headers, verify=self.verify, timeout=10)
eth_data = eth_resp.json()
for eth_mem in eth_data["Members"]:
eth_detail = requests.get(f"https://{self.bmc_ip}{eth_mem['@odata.id']}", headers=self.headers, verify=self.verify, timeout=10)
eth_info = eth_detail.json()
mac = eth_info.get("MACAddress")
ip_addr = eth_info.get("IPv4Addresses", [{}])[0].get("Address")
print(f"BMC FW:{fw_version}, MAC:{mac}, IP:{ip_addr}")
return m_data
def get_system_info(self):
url = f"https://{self.bmc_ip}/redfish/v1/Systems"
resp = requests.get(url, headers=self.headers, verify=self.verify, timeout=10)
data = resp.json()
sys_url = data["Members"][0]["@odata.id"]
sys_resp = requests.get(f"https://{self.bmc_ip}{sys_url}", headers=self.headers, verify=self.verify, timeout=10)
sys_data = sys_resp.json()
sn = sys_data.get("SerialNumber")
model = sys_data.get("Model")
vendor = sys_data.get("Manufacturer")
print(f"Server SN:{sn}, Model:{model}, Vendor:{vendor}")
return sys_data
if __name__ == "__main__":
client = RedfishClient("10.0.0.120", "admin", "Admin@123")
client.get_manager_info()
client.get_system_info()
方案2:IPMI(兼容老服务器,IPMI 2.0 lanplus)
UDP 623端口。
限制:IPMI是老协议,数据字段少,SN、硬件信息经常不全;传感器采集够用。
开发两种实现方式二选一:
- 调用
ipmitool子进程,解析文本输出(快速开发,但是文本解析容易厂商适配坑) - 使用SDK:pyipmi / go ipmi库,直接协议通信(生产推荐)
ipmitool远程采集示例(后端调用shell)
# 获取BMC信息
ipmitool -I lanplus -H BMC_IP -U admin -P password mc info
# 获取BMC网卡
ipmitool -I lanplus -H BMC_IP -U admin -P password lan print
# 获取传感器
ipmitool -I lanplus -H BMC_IP -U admin -P password sdr type temperature
Python subprocess调用ipmitool示例:
import subprocess
def ipmitool_run(host, user, pwd, cmd):
args = ["ipmitool", "-I", "lanplus", "-H", host, "-U", user, "-P", pwd] + cmd
result = subprocess.run(args, capture_output=True, text=True, timeout=8)
return result.stdout, result.stderr
stdout, stderr = ipmitool_run("10.0.0.120", "admin", "Admin@123", ["mc", "info"])
print(stdout)
四、采集任务设计(平台核心)
- 采集任务类型
- 全量资产采集:新增机房上线,一次性拉取BMC资产入库
- 增量定时采集:定时(5~15分钟)拉取传感器、BMC在线状态;不要高频拉取BMC固件信息,BMC固件极少变动
- 并发控制
BMC设备性能弱,单台BMC并发请求多了会拒绝连接。建议:协程/线程池限制并发:10~20并发,单任务超时
10s,失败重试2次,重试间隔2s。 - 账号密码管理
- BMC账号密码敏感,数据库加密存储,禁止明文
- 支持批量导入BMC资产清单(excel:BMC IP、账号、密码、协议)
- 支持账号密码测试连通性(测试接口,新增BMC前验证)
五、异常处理(生产重点)
- 网络不通:BMC不在管理VLAN、ACL拦截443/623
- 账号密码错误:记录错误日志,标记资产异常
- BMC繁忙、超时:重试机制
- 部分厂商Redfish字段不一致:做缺省值兜底,不要直接崩溃
- 证书问题:内网BMC自签证书,代码层面关闭SSL校验;生产环境可导入CA
- 部分老旧BMC只支持IPMI v1.5,lanplus失败,降级尝试lan模式(不安全)
六、安全注意事项
- IPMI协议本身不安全,尽量关闭IPMI over LAN,只使用Redfish HTTPS;
- BMC管理网口独立VLAN,禁止业务网直接访问BMC;
- BMC账号复杂度,不要弱密码;定期轮换密码;
- 平台存储BMC密码:AES加密,密钥单独配置,不要写死代码;
- 审计日志:所有BMC访问、采集操作留日志,谁、何时访问哪个BMC。
七、可选扩展能力(运维平台)
- BMC电源控制:开机/关机/重启(Redfish/IPMI都支持)
- 固件升级任务(Redfish支持上传固件)
- 硬件告警:BMC的SEL系统事件日志采集(故障告警)
- 资产自动发现:C段扫描,探测存活BMC(谨慎,需机房许可)
八、技术选型建议
- Go开发后端服务:推荐,高并发,
gofishRedfish SDK,原生ipmi库,适合平台服务 - Python:适合快速原型开发,
redfish官方库python-redfish-library
官方Redfish库:DMTF python-redfish-library,比手写requests更规范。
如果你继续往下做,我可以:
- 给你 Go语言 gofish 完整采集Demo(生产常用)
- 输出 MySQL建表语句(BMC资产表+传感器表)
- 设计批量导入、连通性测试接口
部分Linux上的操作命令
[root@k8s-slave01 ~]#yum install ipmitool -y
Last metadata expiration check: 2:56:26 ago on Tue 13 Oct 2026 12:25:11 PM CST.
Dependencies resolved.
=====================================================================================================================================================================================================================================
Package Architecture Version Repository Size
=====================================================================================================================================================================================================================================
Installing:
ipmitool x86_64 1.8.18-27.el9 appstream 385 k
Transaction Summary
=====================================================================================================================================================================================================================================
Install 1 Package
Total download size: 385 k
Installed size: 1.0 M
Downloading Packages:
ipmitool-1.8.18-27.el9.x86_64.rpm 777 kB/s | 385 kB 00:00
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 773 kB/s | 385 kB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : ipmitool-1.8.18-27.el9.x86_64 1/1
Running scriptlet: ipmitool-1.8.18-27.el9.x86_64 1/1
Verifying : ipmitool-1.8.18-27.el9.x86_64 1/1
Installed:
ipmitool-1.8.18-27.el9.x86_64
Complete!
[root@k8s-slave01 ~]#modprobe ipmi_devintf
[root@k8s-slave01 ~]#modprobe ipmi_si
[root@k8s-slave01 ~]#ipmitool mc info
Device ID : 32
Device Revision : 1
Firmware Revision : 4.26
IPMI Version : 2.0
Manufacturer ID : 37945
Manufacturer Name : Unknown (0x9439)
Product ID : 43707 (0xaabb)
Product Name : Unknown (0xAABB)
Device Available : yes
Provides Device SDRs : no
Additional Device Support :
Sensor Device
SDR Repository Device
SEL Device
FRU Inventory Device
IPMB Event Receiver
IPMB Event Generator
Chassis Device
Aux Firmware Rev Info :
0x00
0x00
0x00
0x00
[root@k8s-slave01 ~]#ipmitool lan print
Set in Progress : Set Complete
Auth Type Support : MD2 MD5 PASSWORD
Auth Type Enable : Callback : MD2 MD5 PASSWORD
: User : MD2 MD5 PASSWORD
: Operator : MD2 MD5 PASSWORD
: Admin : MD2 MD5 PASSWORD
: OEM :
IP Address Source : Static Address
IP Address : 0.0.0.0
Subnet Mask : 0.0.0.0
MAC Address : 6c:92:bf:6e:69:b9
SNMP Community String : Inspur
IP Header : TTL=0x40 Flags=0x40 Precedence=0x00 TOS=0x10
BMC ARP Control : ARP Responses Enabled, Gratuitous ARP Disabled
Gratituous ARP Intrvl : 0.0 seconds
Default Gateway IP : 0.0.0.0
Default Gateway MAC : 00:00:00:00:00:00
Backup Gateway IP : 0.0.0.0
Backup Gateway MAC : 00:00:00:00:00:00
802.1q VLAN ID : Disabled
802.1q VLAN Priority : 0
RMCP+ Cipher Suites : 1,2,3,6,7,8,11,12
Cipher Suite Priv Max : aaaaXXaaaXXaaXX
: X=Cipher Suite Unused
: c=CALLBACK
: u=USER
: o=OPERATOR
: a=ADMIN
: O=OEM
Bad Password Threshold : 0
Invalid password disable: no
Attempt Count Reset Int.: 0
User Lockout Interval : 0
[root@k8s-slave01 ~]#ipmitool channel info
Channel 0xf info:
Channel Medium Type : System Interface
Channel Protocol Type : KCS
Session Support : session-less
Active Session Count : 0
Protocol Vendor ID : 7154
[root@k8s-slave01 ~]#ipmitool --help
ipmitool: invalid option -- '-'
ipmitool version 1.8.18-csv
usage: ipmitool [options...] <command>
-h This help
-V Show version information
-v Verbose (can use multiple times)
-c Display output in comma separated format
-d N Specify a /dev/ipmiN device to use (default=0)
-I intf Interface to use
-H hostname Remote host name for LAN interface
-p port Remote RMCP port [default=623]
-U username Remote session username
-f file Read remote session password from file
-z size Change Size of Communication Channel (OEM)
-S sdr Use local file for remote SDR cache
-D tty:b[:s] Specify the serial device, baud rate to use
and, optionally, specify that interface is the system one
-4 Use only IPv4
-6 Use only IPv6
-a Prompt for remote password
-Y Prompt for the Kg key for IPMIv2 authentication
-e char Set SOL escape character
-C ciphersuite Cipher suite to be used by lanplus interface
-k key Use Kg key for IPMIv2 authentication
-y hex_key Use hexadecimal-encoded Kg key for IPMIv2 authentication
-L level Remote session privilege level [default=ADMINISTRATOR]
Append a '+' to use name/privilege lookup in RAKP1
-A authtype Force use of auth type NONE, PASSWORD, MD2, MD5 or OEM
-P password Remote session password
-E Read password from IPMI_PASSWORD environment variable
-K Read kgkey from IPMI_KGKEY environment variable
-m address Set local IPMB address
-b channel Set destination channel for bridged request
-t address Bridge request to remote target address
-B channel Set transit channel for bridged request (dual bridge)
-T address Set transit address for bridge request (dual bridge)
-l lun Set destination lun for raw commands
-o oemtype Setup for OEM (use 'list' to see available OEM types)
-O seloem Use file for OEM SEL event descriptions
-N seconds Specify timeout for lan [default=2] / lanplus [default=1] interface
-R retry Set the number of retries for lan/lanplus interface [default=4]
Interfaces:
open Linux OpenIPMI Interface [default]
imb Intel IMB Interface
lan IPMI v1.5 LAN Interface
lanplus IPMI v2.0 RMCP+ LAN Interface
serial-terminal Serial Interface, Terminal Mode
serial-basic Serial Interface, Basic Mode
usb IPMI USB Interface(OEM Interface for AMI Devices)
Commands:
raw Send a RAW IPMI request and print response
i2c Send an I2C Master Write-Read command and print response
spd Print SPD info from remote I2C device
lan Configure LAN Channels
chassis Get chassis status and set power state
power Shortcut to chassis power commands
event Send pre-defined events to MC
mc Management Controller status and global enables
sdr Print Sensor Data Repository entries and readings
sensor Print detailed sensor information
fru Print built-in FRU and scan SDR for FRU locators
gendev Read/Write Device associated with Generic Device locators sdr
sel Print System Event Log (SEL)
pef Configure Platform Event Filtering (PEF)
sol Configure and connect IPMIv2.0 Serial-over-LAN
tsol Configure and connect with Tyan IPMIv1.5 Serial-over-LAN
isol Configure IPMIv1.5 Serial-over-LAN
user Configure Management Controller users
channel Configure Management Controller channels
session Print session information
dcmi Data Center Management Interface
nm Node Manager Interface
sunoem OEM Commands for Sun servers
kontronoem OEM Commands for Kontron devices
picmg Run a PICMG/ATCA extended cmd
fwum Update IPMC using Kontron OEM Firmware Update Manager
firewall Configure Firmware Firewall
delloem OEM Commands for Dell systems
shell Launch interactive IPMI shell
exec Run list of commands from file
set Set runtime variable for shell and exec
hpm Update HPM components using PICMG HPM.1 file
ekanalyzer run FRU-Ekeying analyzer using FRU files
ime Update Intel Manageability Engine Firmware
vita Run a VITA 46.11 extended cmd
lan6 Configure IPv6 LAN Channels
[root@k8s-slave01 ~]#
tbd
赠人玫瑰
手留余香
我们曾如此渴望命运的波澜,到最后才发现:人生最曼妙的风景,竟是内心的淡定与从容……我们曾如此期盼外界的认可,到最后才知道:世界是自己的,与他人毫无关系!-杨绛先生
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【关注我】。

浙公网安备 33010602011771号