跳到主要内容

4、ipmi批量操作服务器.md

前提

具备python环境,且具备impi软件环境;(windows版ipmi记得要把其路径添加到环境变量里去)

1、查看机器电源状态

v1-原始版

ip.txt

/服务器带外ip/
/服务器带外ip/

ipmi_powerStatus.py

import os
import time
for i in open(r'ip.txt'):
ip=i.split("/")[1]
print(ip)
os.system("ipmitool -I lanplus -H "+ ip + " -U 服务器带外账号 -P 服务器带外密码 power status")
time.sleep(1)
time.sleep(100)

v2-增加单条impi命令-2023.2.28

ip.txt

https://onedayxyy.cn/scripts/ipmi/ipmi_power_status/v2-add-single-command-2023.3.28/ip.txt

/服务器带外ip/
/服务器带外ip/

ipmi_powerStatus.py

https://onedayxyy.cn/scripts/ipmi/ipmi_power_status/v2-add-single-command-2023.3.28/ipmi_powerStatus.py

import os
import time
for i in open(r'ip.txt'):
ip=i.split("/")[1]
print(ip)
os.system("ipmitool -I lanplus -H "+ ip + " -U admin -P 服务器带外密码 power status")
# ipmitool -I lanplus -H 10.10.10.10 -U admin -P "服务器带外密码" power status
time.sleep(1)
time.sleep(100)

2、批量从pxe启动脚本

v1-2023.11.23

ip.txt

https://onedayxyy.cn/scripts/ipmi/ipmi_pxe/v1-2023.11.23/ip.txt

/服务器带外地址/

ipmi_pxe.py

https://onedayxyy.cn/scripts/ipmi/ipmi_pxe/v1-2023.11.23/ipmi_pxe.py

import os
import time
for i in open(r'ip.txt'):
ip=i.split("/")[1]
print(ip)
os.system("ipmitool -I lanplus -H "+ ip + " -U admin -P 123456 chassis bootdev pxe") #从PXE启动
time.sleep(1)
os.system("ipmitool -I lanplus -H "+ ip + " -U admin -P 123456 power reset") #重启
time.sleep(100)

shell版从pxe启动

  • 代码:
[root@docusaurus-wiki shell_pxe]# cat ip.txt 
/物理机带外ip/
/物理机带外ip/[root@docusaurus-wiki shell_pxe]# cat ipmi_pxe_shell.sh
#!/bin/bash
for i in `cat /root/ipmi_ip.txt`
do
impitool -H "$i" -I lanplus -U root -P 123456 chassis bootdev pxe && impitool -H "$i" -I lanplus -U root -P 123456 power reset &> /dev/null
if [ $? -ne 0 ]; then
echo $i Error
else
echo &i OK
done[root@docusaurus-wiki shell_pxe]#
  • 代码位置:

https://onedayxyy.cn/scripts/ipmi/ipmi_pxe/shell_pxe/

image-20240327124916340

3、批量开关机和重启

v1-2023.11.23

ip.txt

https://onedayxyy.cn/scripts/ipmi/ipmi_powerOn_poweroff_reset/v1-2023.11.23/ip.txt

/服务器带外地址/

ipmi_pxe.py

https://onedayxyy.cn/scripts/ipmi/ipmi_powerOn_poweroff_reset/v1-2023.11.23/ipmi_pxe.py

import os
import time
for i in open(r'ip.txt'):
ip=i.split("/")[1]
print(ip)
# os.system("ipmitool -I lanplus -H "+ ip + " -U admin -P 123456 chassis bootdev pxe") #从PXE启动
# time.sleep(1)
os.system("ipmitool -I lanplus -H "+ ip + " -U admin -P 123456 power reset") #重启
os.system("ipmitool -I lanplus -H "+ ip + " -U admin -P 123456 power on") #开机
os.system("ipmitool -I lanplus -H "+ ip + " -U admin -P 123456 power off") #关机
time.sleep(100)

4、批量修改服务器带外密码

v1-2023.11.23

ip.txt

https://onedayxyy.cn/scripts/ipmi/ipmi_modify_daiwai_password/v1-2023.11.23/ip.txt

/服务器带外地址/

ipmi_password.py

https://onedayxyy.cn/scripts/ipmi/ipmi_modify_daiwai_password/v1-2023.11.23/ipmi_password.py

import os
import time
for i in open(r'ip.txt'):
ip=i.split("/")[1]
print(ip)
os.system("ipmitool -H "+ ip + " -I lanplus -U admin -P 123456 user set password <user id> 新密码")
time.sleep(1)
time.sleep(100)

v2-2024.4.5

脚本位置:

https://onedayxyy.cn/scripts/ipmi/ipmi_modify_daiwai_password/v2-2024.4.5/

[root@docusaurus-wiki v2-2024.4.5]#ll
total 16
-rw-rw-rw- 1 root root 1418 Nov 17 2021 changePassword.py
-rw-rw-rw- 1 root root 10268 Apr 5 14:32 shanghai.xlsx
[root@docusaurus-wiki v2-2024.4.5]#

changePassword.py

import os
import xlrd


def read_File():
result = []
data = xlrd.open_workbook('shanghai.xlsx')
table = data.sheets()[0]
rowNum = table.nrows
for j in range(1,rowNum):
cj = table.cell_value(j, 0)
addr = table.cell_value(j, 2)
userName = table.cell_value(j, 3)
passWord = table.cell_value(j, 4)
newPassWord = table.cell_value(j, 5)
result.append([cj, addr, userName, passWord, newPassWord])
return result

def change_Password():
data = read_File()
for j in data:
if j[0] =='浪潮':
res =os.popen('ipmitool -I lanplus -H '+j[1]+' -U ' +j[2]+' -P '+j[3]+' user set password 1 '+j[4]).read()
print(type(res))
if 'successful' in res:
print(j[1] + ' change Password successful')
else:
print(j[1] + ' change Password fail')
elif j[0] =='烽火' or j[0] == '曙光' or j[0] =='华为':
res =os.popen('ipmitool -I lanplus -H '+j[1]+' -U ' +j[2]+' -P '+j[3]+' user set password 2 '+j[4]).read()
if 'successful' in res:
print(j[1] + ' change Password successful')
else:
print(j[1] + ' change Password fail')
else:
print(j[1]+ f' 配置失败{j[0]}设备不在支持范围内')

if __name__ == '__main__':
change_Password()

shanghai.xlsx

image-20240405143431778

5、dell服务器修改带外ntp脚本

  • 脚本位置

https://onedayxyy.cn/scripts/dell_Server_Modify_NtpServer/

image-20240329125656395

ip.txt

服务器带外地址

DELLradamNtp.py

import os
import time
import paramiko

for i in open(r'ip.txt'):
ip=i.replace('\n','')
print(ip)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ip, port=22, username='root',password='123456')
stdin,stdout,stderr = ssh.exec_command("racadm set iDRAC.ntPConfigGroup.NTP1 192.168.1.1")
stdin,stdout,stderr = ssh.exec_command("racadm set iDRAC.ntPConfigGroup.NTP2 192.168.1.2")
#stdin,stdout,stderr = ssh.exec_command("testtrap -i 1")
#stdin,stdout,stderr = ssh.exec_command("testtrap -i 2")
ssh.close()
print("ok")
time.sleep(100)
  • 测试环境:

R730

c6320

racadm>>racadm set iDRAC.ntPConfigGroup.NTP2 192.168.1.1
[Key=iDRAC.Embedded.1#NTPConfigGroup.1]
Object value modified successfully
racadm>>
racadm>>
racadm>>racadm set iDRAC.ntPConfigGroup.NTP1 192.168.1.2
[Key=iDRAC.Embedded.1#NTPConfigGroup.1]
Object value modified successfully
racadm>>
racadm>>
  • 进测试:是可以生效的。

  • 脚本需优化:认证失败的话,脚本就退出了。。。

image-20220804093202727

  • 验证

image-20240329125124353