正文:
今天写了两个python多线程批处理管理服务器的脚本。
IPMI(Intelligent Platform Management Interface,智能平台管理接口)是应用于服务器管理系统设计的标准,由 Intel、HP、Dell 和 NEC 于 1998 年联合提出。IPMI 的主要特点是它可以独立于处理器、BIOS 和操作系统。使用该标准,有助于在不同类型的服务器系统硬件上实现系统管理,并使不同平台的集中管理成为可能。
在 IPMI 管理平台中,BMC(基板管理控制器)是核心控制器,通过与 BMC 的通信实现系统管理软件对各个设备的管理。BMC 与主处理器和板上的组件连接,并监控或管理物理组件。
由于 BMC 系统的独立性,IPMI 为高可用性(HA)系统提供了企业级管理工具,即使在系统断电状态下也可以使用平台管理功能。当系统管理软件和正常的带内管理机制不可用时,可以获取平台状态信息并开始恢复操作。通过 IPMI 独立的监控、日志记录等功能,它为服务器硬件提供了一个内置的可管理平台。
首先看下运行界面
版本 BMC 管理脚本一
// https://www.huizhanii.com
import os
import subprocess
import time
from queue import Queue
from threading import Thread
class IPMI_Manaeger:
# 任务队列
def __init__(self):
# ip 列表
self.ipaddress_queue=Queue()
self.IPMI_tool_queue = Queue()
def IP_list(self):
with open("ip_list.txt", "r", encoding="utf-8") as f:
ip_address = f.readlines()
self.ipaddress_queue.put(ip_address)
def IPMI(self, choose_id, user, pwd):
ipaddress_list=self.ipaddress_queue.get()
if choose_id == 1:
for ipaddress in ipaddress_list:
#self.IPMI_tool_queue.put(subprocess.run('dir',shell=True))
# self.IPMI_tool_queue.put(os.system( "<a target="_blank" href="https://www.huizhanii.com/tag/ipmi" title="View all posts in ipmi" rel="noopener">ipmi</a>tool -I lanplus -H " + ipaddress + " -U " + user + " -P " + pwd + " power status"))
self.IPMI_tool_queue.put(subprocess.Popen("ipmitool -I lanplus -H " + ipaddress + " -U " + user + " -P " + pwd + " power status",shell=True))
elif choose_id == 2:
for ipaddress in ipaddress_list:
self.IPMI_tool_queue.put(subprocess.Popen("ipmitool -I lanplus -H " + ipaddress + " -U " + user + " -P " + pwd + " power on",shell=True))
elif choose_id == 3:
for ipaddress in ipaddress_list:
self.IPMI_tool_queue.put(subprocess.Popen("ipmitool -I lanplus -H " + ipaddress + " -U " + user + " -P " + pwd + " power off",shell=True))
self.ipaddress_queue.task_done()
elif choose_id == 4:
for ipaddress in ipaddress_list:
self.IPMI_tool_queue.put(subprocess.Popen("ipmitool -I lanplus -H " + ipaddress + " -U " + user + " -P " + pwd + " power reset",shell=True))
elif choose_id == 5:
for ipaddress in ipaddress_list:
self.IPMI_tool_queue.put(subprocess.Popen("ipmitool -I lanplus -H " + ipaddress + " -U " + user + " -P " + pwd + " chassis bootdev pxe",shell=True))
elif choose_id == 6:
for ipaddress in ipaddress_list:
self.IPMI_tool_queue.put(subprocess.Popen("ipmitool -I lanplus -H " + ipaddress + " -U " + user + " -P " + pwd + " chassis bootdev bios",shell=True))
elif choose_id == 7:
for ipaddress in ipaddress_list:
self.IPMI_tool_queue.put(subprocess.Popen("ipmitool -I lanplus -H " + ipaddress + " -U " + user + " -P " + pwd + " chassis bootdev cdrom",shell=True))
elif choose_id == 8:
for ipaddress in ipaddress_list:
self.IPMI_tool_queue.put(subprocess.Popen("ipmitool -I lanplus -H " + ipaddress + " -U " + user + " -P " + pwd + " chassis bootdev disk",shell=True))
elif choose_id == 9:
for ipaddress in ipaddress_list:
self.IPMI_tool_queue.put(subprocess.Popen("ipmitool -I lanplus -H " + ipaddress + " -U " + user + " -P " + pwd + " mc reset cold",shell=True))
elif choose_id == 10:
for ipaddress in ipaddress_list:
self.IPMI_tool_queue.put(subprocess.Popen("ipmitool -I lanplus -H " + ipaddress + " -U " + user + " -P " + pwd + " mc reset warm",shell=True))
elif choose_id == 11:
for ipaddress in ipaddress_list:
self.IPMI_tool_queue.put(subprocess.Popen("ipmitool -I lanplus -H " + ipaddress + " -U " + user + " -P " + pwd + " power off"& time.sleep(
5) & "ipmitool -I lanplus -H " + ipaddress + " -U " + user + " -P " + pwd + " power on",shell=True))
self.ipaddress_queue.task_done()
def IPMI_run(self):
print("***************************************")
print("1.查看电源状态")
print("2.开机")
print("3.关机")
print("4.重启")
print("5.PXE 启动")
print("6.BIOS 启动")
print("7.从 CD/DVD 启动")
print("8.从硬盘启动")
print("9.冷重启 BMC")
print("10.热重启 BMC")
print("11.冷重启系统")
print("***************************************")
choose_id = int(input("请选择需要执行指令 ID:"))
username = input("请输入 BMC 用户名:")
password = input("请输入 BMC 密码:")
IMPI_list = []
IPMI_ip = Thread(target=self.IP_list())
IMPI_list.append(IPMI_ip)
#print(IMPI_list)
for i in range(5):
IPMI_tools = Thread(target=self.IPMI, args=(choose_id, username, password))
IMPI_list.append(IPMI_tools)
for t in IMPI_list:
t.setDaemon(True)
t.start()
time.sleep(5)
print(IMPI_list)
for q in [self.IPMI_tool_queue,self.ipaddress_queue]:
q.join()
if __name__ == '__main__':
# 消费线程
IPMI=IPMI_Manaeger()
IPMI.IPMI_run()
版本 BMC 管理脚本二
// https://www.huizhanii.com
import os
import time
import subprocess
from queue import Queue
from threading import Thread
class IPMI_Manager:
def __init__(self):
self.ipaddress_queue = Queue()
self.IPMI_tool_queue = Queue()
def IP_list(self):
with open("ip_list.txt", "r", encoding="utf-8") as f:
ip_address = f.readlines()
self.ipaddress_queue.put(ip_address)
def IPMI(self, choose_id, user, pwd):
ipaddress_list = self.ipaddress_queue.get()
for ipaddress in ipaddress_list:
command = f"ipmitool -I lanplus -H {ipaddress.strip()} -U {user} -P {pwd}"
if choose_id == 1:
command += " power status"
elif choose_id == 2:
command += " power on"
elif choose_id == 3:
command += " power off"
elif choose_id == 4:
command += " power reset"
elif choose_id == 5:
command += " chassis bootdev pxe"
elif choose_id == 6:
command += " chassis bootdev bios"
elif choose_id == 7:
command += " chassis bootdev cdrom"
elif choose_id == 8:
command += " chassis bootdev disk"
elif choose_id == 9:
command += " mc reset cold"
elif choose_id == 10:
command += " mc reset warm"
elif choose_id == 11:
command += " power off && sleep 5 && ipmitool -I lanplus -H {ipaddress.strip()} -U {user} -P {pwd} power on"
process = subprocess.run(command, shell=True)
self.IPMI_tool_queue.put(process)
self.ipaddress_queue.task_done()
def IPMI_run(self):
print("***************************************")
print("1. 查看电源状态")
print("2. 开机")
print("3. 关机")
print("4. 重启")
print("5. PXE 启动")
print("6. BIOS 启动")
print("7. 从 CD/DVD 启动")
print("8. 从硬盘启动")
print("9. 冷重启 BMC")
print("10. 热重启 BMC")
print("11. 冷重启系统")
print("***************************************")
choose_id = int(input("请选择需要执行指令 ID: "))
username = input("请输入 BMC 用户名: ")
password = input("请输入 BMC 密码: ")
IMPI_list = []
IPMI_ip = Thread(target=self.IP_list)
IMPI_list.append(IPMI_ip)
for _ in range(4):
IPMI_tools = Thread(target=self.IPMI, args=(choose_id, username, password))
IMPI_list.append(IPMI_tools)
for t in IMPI_list:
print(t)
t.setDaemon(True)
t.start()
for q in [self.IPMI_tool_queue, self.ipaddress_queue]:
q.join()
if __name__ == '__main__':
IPMI = IPMI_Manager()
IPMI.IPMI_run()
转载请注明:汇站网 » Python 多线程批处理管理服务器脚本