背景
近期收到IDC团队的通知,可以将IT团队的一些老服务器进行批量更换,可以统计一下要更换的老服务器。 可是公司的服务器都是IDC团队分发的,IT从来没有做过序列号和生命周期的管理,怎么统计出这些服务器呢。
思路
服务器出厂日期是最直观的,可是我们又没有每台服务器的SN,这么多服务器怎么获取这些SN呢。总不能真的一台一台登陆远控卡去查询吧。
因此,我们需要想办法批量获取这些服务器的SN。然后根据SN去固资部门或者官网查询出厂日期。
IT这边的服务器运行的清一色windows server,所以可以利用的手段有:
- IPMItool工具
- PowerShell脚本
实现
方式一:IPMItool
我们首先通过impitool获取服务器的fru信息,在考虑从中过滤出SN,如图中绿色标注部分。
我们写个python脚本:
#!/bin/env python3
import os
iplists = ['172.16.xxx.xxx','172.16.xxx.xxx','172.16.xxx.xxx','172.16.xxx.xxx','172.16.xxx.xxx','172.16.xxx.xxx']
for ip in iplists:
try:
dict1= {}
datas = os.popen('ipmitool -H %s -I lanplus -Uusername -Ppassword -L user fru print 0'%ip).readlines()
for i in datas:
j = i.strip('\n')
key, value = j.split(':',1)[0].strip(),j.split(':',1)[-1].strip()
dict1[key] = value
print(dict1['Product Manufacturer'],ip,dict1['Product Serial'])
except:
print('{} 获取失败!'.format(ip))
执行结果:
大部分都正常获取到了,失败的两台是因为其中1台密码不同,另1台没有在LAN上启用IPMI功能。
方式二: PowerShell
通过wmiobject 获取bios信息,从而进一步获取SN信息。
最终形成脚本:
$hostlists = "BJ-x-Hyper-V01","BJ-x-Hyper-V02","BJ-x-Hyper-V03","BJ-x-Hyper-V04"
Foreach($h in $hostlists){
$info = Invoke-Command -ComputerName $h -ScriptBlock { Get-WmiObject -Class Win32_BIOS}
$sn = $info.SerialNumber
$man = $info.Manufacturer
Write-Output "$h $man $sn"
}
执行结果:
获取到SN之后,就可以进一步查询服务器的生命周期了,同时也进一步完善了服务器管理信息。
脚本中,我们只需维护一个IP或主机列表。列表也可以通过命令去生成。