跳到主要内容
v3-2025.7.4-输出控制台同时保存到csv
python脚本 ipmi

v3-2025.7.4-输出控制台同时保存到csv

345 字 2 分钟 -- 阅读
最后更新于:
AI 摘要

v3-2025.7.4-输出控制台同时保存到csv

测试成功。

脚本位置:

D:\BaiduSyncdisk\脚本\01-ipmi脚本\查看机器电源状态\v3-2025.7.4-输出控制台同时保存到csv-测试成功

image-20250726154853532

脚本详情:

ip.txt

1/192.168.1.1/
2/192.168.1.2/

ipmi.py

 1import os
 2import time
 3import csv
 4from datetime import datetime
 5
 6# 配置文件路径
 7IP_FILE = 'ip.txt'
 8OUTPUT_CSV = f'ipmi_results_{datetime.now().strftime("%Y%m%d_%H%M%S")}.csv'
 9
10# IPMI 认证信息(建议改为从配置文件或环境变量读取敏感信息)
11IPMI_USER = "admin"
12IPMI_PASS = "123456"
13
14def check_power_status(ip):
15    """执行IPMI电源状态检查并返回结果"""
16    cmd = f'ipmitool -I lanplus -H {ip} -U {IPMI_USER} -P {IPMI_PASS} power status'
17    try:
18        result = os.popen(cmd).read().strip()
19        return result if result else "No response"
20    except Exception as e:
21        return f"Error: {str(e)}"
22
23def main():
24    # 创建CSV文件并写入表头
25    with open(OUTPUT_CSV, 'w', newline='') as csvfile:
26        writer = csv.writer(csvfile)
27        writer.writerow(['IP Address', 'Power Status', 'Timestamp'])
28        
29        # 处理每个IP
30        with open(IP_FILE) as f:
31            for line in f:
32                if not line.strip():  # 跳过空行
33                    continue
34                    
35                ip = line.strip().split("/")[1]
36                print(f"\nChecking IP: {ip}")  # 控制台输出
37                
38                # 获取电源状态
39                status = check_power_status(ip)
40                timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
41                
42                # 控制台打印结果
43                print(f"  Status: {status}")
44                print(f"  Timestamp: {timestamp}")
45                
46                # 写入CSV文件
47                writer.writerow([ip, status, timestamp])
48                
49                time.sleep(1)  # 防止IPMI服务阻塞
50
51    print(f"\n[SUCCESS] 所有检测完成!结果已保存到: {OUTPUT_CSV}")
52
53if __name__ == "__main__":
54    main()
最新文章

本页导航

文档导航