如何编写Python程序从IPMI响应提取十六进制值并转ASCII字符串
IPMI输出十六进制值提取转ASCII方案
你可以通过逐行匹配关键词+字符串分割的方式提取目标内容,完整可运行代码如下:
# 模拟你从命令行拿到的完整输出,实际使用时可以替换为subprocess拿到的stdout内容 cmd_output = """$> set system cmd -i 33 -c raw 0x38 0x0b 0x06 -- command Ipmi Response: 31 2e 30 38 2e 32 30 32 31 30 38 32 35 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 Completion Code: Success """ target_hex = None # 逐行遍历匹配目标行 for line in cmd_output.splitlines(): stripped_line = line.strip() # 不区分大小写匹配前缀,兼容性更强 if stripped_line.lower().startswith("ipmi response:"): # 仅按第一个冒号分割,避免内容异常导致截取错误 target_hex = stripped_line.split(":", 1)[1].strip() break if target_hex: # 转ASCII同时去掉末尾的空字符 ascii_result = ''.join(chr(int(i, 16)) for i in target_hex.split()).rstrip('\x00') print(f"转换结果:{ascii_result}") else: print("未匹配到Ipmi Response对应内容")
如果你需要直接在Python中执行IPMI命令获取输出,可以用subprocess模块实现:
import subprocess cmd = ["set", "system", "cmd", "-i", "33", "-c", "raw", "0x38", "0x0b", "0x06", "--", "command"] run_result = subprocess.run(cmd, capture_output=True, text=True) cmd_output = run_result.stdout
示例运行后输出结果为1.08.20210825,和你提供的十六进制串对应内容一致。
内容的提问来源于stack exchange,提问作者jpb38
相关产品推荐
相关产品推荐

