Python多线程批量管理服务器脚本

大家好,我是不念,今天编写了两个python多线程批量管理服务器的脚本。

IPMI(Intelligent Platform Management Interface,智能平台管理接口)是一项应用于服务器管理系统设计的标准,由Intel、HP、Dell和NEC公司于1998年共同提出。IPMI 的主要特性是可独立于处理器、BIOS 和操作系统,利用此标准,有助于在不同类服务器系统硬件上实施系统管理,使不同平台的集中管理成为可能。

在IPMI管理平台中,BMC(Baseboard Management Controller,基板管理控制器)是核心控制器,系统管理软件对各个器件的管理都是通过与BMC通信来实现的。BMC与主处理器和板上各元件相连接,监控或管理各物理部件。

由于BMC系统的独立性,使得IPMI为高可用性 (HA) 系统提供了一种企业级的管理手段,甚至在系统处于电源关闭状态时,平台管理功能也可使用。在系统管理软件和正常带内管理机制都不可用的情况下可获得平台状态信息并启动恢复操作。通过IPMI独立监视、日志记录等功能,为服务器硬件提供了内置的可管理平台。

版本BMC管理脚本一

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( "ipmitool -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管理脚本二

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()

运行界面:

图片[1]-Python多线程批量管理服务器脚本-不念博客
© 版权声明
THE END