收集主机信息,包括:
1、主机名:hostname
2、IP地址:ip
3、操作系统版本:osver
4、服务器厂商:vendor
5、服务器型号:product
6、服务器序列号:sn
7、cpu型号:cpu_module
8、cpu核心数量:cpu_num
9、内存大小
收集IP信息
[[email protected] systeminformation]# vim sysinformation.py from subprocess import Popen,PIPE def getIfconfig(): p = Popen([‘ifconfig‘],stdout=PIPE) data = p.stdout.read() return data def getDmi(): p = Popen([‘dmidecode‘],stdout = PIPE) data = p.stdout.read() return data def parseData(data): parsed_data = [] new_line = ‘‘ data = [i for i in data.split(‘\n‘) if i ] for line in data: if line[0].strip(): parsed_data.append(new_line) new_line = line + ‘\n‘ else: new_line +=line + ‘\n‘ parsed_data.append(new_line) return parsed_data def parseIfconfig(parsed_data): dic = {} tuple_addr= (‘lo‘,‘vir‘,‘vnet‘,‘em3‘,‘em4‘) parsed_data = [i for i in parsed_data if i and not i.startswith(‘tuple_addr‘)] for lines in parsed_data: line_list = lines.split(‘\n‘) devname = line_list[0].split()[0] macaddr = line_list[0].split()[-1] ipaddr = line_list[1].split()[1].split(‘:‘)[1] break dic [‘ip‘] = [devname, ipaddr, macaddr] return dic if __name__ == "__main__": data = getIfconfig() parsed_data = parseData(data) print parseIfconfig(parsed_data) [[email protected] systeminformation]# python sysinformation.py {‘ip‘: [‘br1‘, ‘112.65.140.133‘, ‘A4:BA:DB:20:93:23‘]}
时间: 2024-12-25 15:21:16