python获取服务器基本信息的脚本

 

之前我发布了批量收集服务器信息脚本,本次更新优化了脚本,对脚本设置了try catch 抛出异常等等,收集信息如下:

ip地址","主机名","序列号","服务器型号","系统盘数量", "系统盘符","系统盘容量","系统盘分区","硬盘列表","硬盘数量","CPU核数", "CPU型号","CPU频率","内存大小","内存条数量","内存型号","系统版本","BIOS模式","内核版本","字符集","软件包数量",'网卡Down','网卡Up',"网卡数量",'网卡10M','网卡100M','网卡1000M','网卡10000M','网卡型号','NUMA开启状态','CPU-NUMA状态','网卡NUMA状态',"服务器IPMI用户数量","IPMI地址","IPMI掩码","IPMI版本

 

 

# 导入paramiko,openpyxl和threading库
import paramiko
import openpyxl
import threading
import xlwt


# 定义一个函数,用于连接远程Linux系统并执行命令
def ssh_exec(ip,cmd):
# 创建SSHClient实例对象
ssh = paramiko.SSHClient()
# 设置自动添加主机密钥
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接远程主机
try:
ssh.connect(ip, 22, username='root',password='redhat')
except Exception:
result = ip + " 连接失败!"
# 执行命令并获取输出
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read().decode()
# 关闭连接
ssh.close()
# 返回结果
return result

# 定义一个函数,用于解析结果,并写入工作表中
def write_info(ws, line):
# 去掉换行符,并用逗号分隔内容
line = line.strip().split(",")
# 获取IP
ip = line[0]
print(f"连接到{ip}...")
row = []
row.append(ip)
try:
result = ssh_exec(ip, cmd)
# 按行分隔结果,并去掉第一行和最后一行(提示信息)
result = result.split("^")[1:-1]
# 定义一个空列表,用于存储解析后的信息
# 遍历每行结果,按冒号分隔内容,并取第二部分作为信息
for r in result:
# info = r.split(",").split()
row.append(r)
# 将这一行写入工作表中
ws.append(row)
except:
row.append("连接失败!")
ws.append(row)

 

# 定义一个命令,用于在远程Linux系统上获取各种信息,包括IP地址、主机名、服务器序列号、
# 服务器型号、硬盘数量、系统盘符、系统分区、硬盘列表、CPU核数、CPU型号、CPU频率、内存大小,
# 内存条数量,内存条参数,系统版本、BIOS模式、内核版本,字符集、软件包数量、Bond状态、网卡型号、
# 服务器IPMI用户数量,IPMI地址,IPMI掩码,IPMI版本,系统网卡状态信息
cmd = """
echo "$(hostname -I)"^
echo "$(hostname)"^
echo "$(dmidecode -s system-serial-number |sed s/[[:space:]]//g)"^
echo "$(dmidecode -s system-product-name)"^
echo "$(lsblk | grep disk | wc -l)"^
echo "$(lsblk -l |grep -i boot -B 1 |grep -i disk |awk ' {print $1}' )"^
echo "$(i=`lsblk | grep -w '/' |cut -c 3-5` && lsblk |grep $i | awk 'NR==1 {print $4}')"^
echo "$(lsblk -f | grep / | awk '{print $1}')"^
echo "$(lsblk -a)"^
echo "$(lsblk | grep disk | wc -l)"^

echo "$(nproc)"核^
echo "$(cat /proc/cpuinfo | grep 'model name' | uniq | cut -d ':' -f 2)"^
echo "$(lscpu | grep 'CPU MHz' | awk '{print $3}') MHz"^
echo "$(free -h | grep Mem | awk '{print $2}')"^
echo "$(dmidecode -t memory | grep Size: | grep 'MB\|GB' | wc -l)"^
echo "$(dmidecode -t memory | grep -i 'Part Number' | grep -v 'NO DIMM'|awk '{print $3}')"^
echo "$(cat /etc/system-release)"^
echo "$(ls /sys/firmware/efi/efivars > /dev/null 2>&1 && echo UEFI || echo Legacy)"^
echo "$(uname -r)"^
echo "$(locale charmap)"^
echo "$(rpm -qa | wc -l)"^
echo "$(for i in `ls /sys/class/net/ | grep 'ens\|enp\|eth'`; do ethtool $i|grep Speed|grep Unk >>/dev/null&&echo $i; done;)"^
echo "$(ip addr | grep UP | grep 'ens\|enp\|eth\|bond' | awk -F : '{print $2}')"^
echo "$(for i in `ls /sys/class/net/ | grep 'ens\|enp\|eth'`; do echo $i `cat /sys/class/net/$i/device/numa_node` | awk -F " " '{print $1}'; done;)"^
echo "$(for i in `ls /sys/class/net/ | grep 'ens\|enp\|eth'`; do ethtool $i|grep Speed|grep 10M >>/dev/null&&echo $i; done;)"^
echo "$(for i in `ls /sys/class/net/ | grep 'ens\|enp\|eth'`; do ethtool $i|grep Speed|grep 100M >>/dev/null&&echo $i; done;)"^
echo "$(for i in `ls /sys/class/net/ | grep 'ens\|enp\|eth'`; do ethtool $i|grep Speed|grep 1000M >>/dev/null&&echo $i; done;)"^
echo "$(for i in `ls /sys/class/net/ | grep 'ens\|enp\|eth'`; do ethtool $i|grep Speed|grep 10000M >>/dev/null&&echo $i; done;)"^
echo "$(lspci | grep Ethernet | awk -F : '{print $3}')"^
echo "$(dmesg | grep -i numa | grep 'No NUMA configuration found')"^
echo "$(lscpu |grep 'NUMA')"^
echo "$(for i in `ls /sys/class/net/ | grep e`; do echo $i `cat /sys/class/net/$i/device/numa_node`; done;)"^

echo "$(ipmitool user list 1 | wc -l)"^
echo "$(ipmitool lan print 1 | grep 'IP Address')"^
echo "$(ipmitool lan print 1 | grep 'Subnet Mask')"^

echo "$(ipmitool mc info | grep 'Firmware Revision')"^
echo "$(ip a)^
"""

# 定义一个文件名,存储要连接的Linux系统的信息,包括IP,用户名,密码,每行一个,用逗号分隔
filename = "linux_list.txt"

# 打开文件,并按行读取内容
with open(filename) as f:
lines = f.readlines()

# 创建一个excel工作簿对象
wb = openpyxl.Workbook()
# 获取当前活动的工作表对象
ws = wb.active
# 设置工作表的标题为"服务器信息"
ws.title = "服务器信息"
# "ip地址","主机名","序列号","服务器型号","系统盘数量",
# "系统盘符","系统盘容量","系统盘分区","硬盘列表","硬盘数量","CPU核数",
# "CPU型号","CPU频率","内存大小","内存条数量","内存型号",
# "系统版本","BIOS模式","内核版本","字符集","软件包数量",'网卡Down','网卡Up',"网卡数量",
#'网卡10M','网卡100M','网卡1000M','网卡10000M','网卡型号','NUMA开启状态','CPU-NUMA状态','网卡NUMA状态',
#"服务器IPMI用户数量","IPMI地址","IPMI掩码","IPMI版本"


ws.append(["ip地址","主机名","序列号","服务器型号","系统盘数量",
"系统盘符","系统盘容量","系统盘分区","硬盘列表","硬盘数量","CPU核数",
"CPU型号","CPU频率","内存大小","内存条数量","内存型号",
"系统版本","BIOS模式","内核版本","字符集","软件包数量",'网卡Down','网卡Up',"网卡数量",
'网卡10M','网卡100M','网卡1000M','网卡10000M','网卡型号','NUMA开启状态','CPU-NUMA状态','网卡NUMA状态',
"服务器IPMI用户数量","IPMI地址","IPMI掩码","IPMI版本"])


for i, val in enumerate(ws):
ws.write(0, i, label=val, style=ws)
first_col = ws.col(i)
first_col.width = 180 * 20

 

# 定义一个空列表,用于存储线程对象
threads = []

# 遍历每行内容,创建一个线程对象,并将其添加到列表中
for line in lines:
t = threading.Thread(target=write_info, args=(ws, line))
threads.append(t)

# 遍历线程列表,启动每个线程
for t in threads:
t.start()

# 遍历线程列表,等待每个线程结束
for t in threads:
t.join()

# 保存工作簿为"server_info.xlsx"
wb.save("server_info.xlsx")

结果:

 

posted @ 2024-03-04 15:47  往事已成昨天  阅读(45)  评论(0编辑  收藏  举报