收集主機信息,包括:
“只有客戶發(fā)展了,才有我們的生存與發(fā)展!”這是創(chuàng)新互聯(lián)公司的服務宗旨!把網(wǎng)站當作互聯(lián)網(wǎng)產(chǎn)品,產(chǎn)品思維更注重全局思維、需求分析和迭代思維,在網(wǎng)站建設(shè)中就是為了建設(shè)一個不僅審美在線,而且實用性極高的網(wǎng)站。創(chuàng)新互聯(lián)對成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)站開發(fā)、網(wǎng)頁設(shè)計、網(wǎng)站優(yōu)化、網(wǎng)絡推廣、探索永無止境。1、主機名:hostname
2、IP地址:ip
3、操作系統(tǒng)版本:osver
4、服務器廠商:vendor
5、服務器型號:product
6、服務器序列號:sn
7、cpu型號:cpu_module
8、cpu核心數(shù)量:cpu_num
9、內(nèi)存大小
收集IP信息
[root@133 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) [root@133 systeminformation]# python sysinformation.py {'ip': ['br1', '112.65.140.133', 'A4:BA:DB:20:93:23']}
收集System Information信息
[root@133 systeminformation]# vim sysinformation.py 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 def parseDmi(parsed_data): dic = {} parsed_data = [i for i in parsed_data if i.startswith('System Information')] return parsed_data if __name__ == "__main__": data_ip = getIfconfig() parsed_data_ip = parseData(data_ip) print parseIfconfig(parsed_data_ip) data_dmi = getDmi() parsed_data_dmi = parseData(data_dmi) print parseDmi(parsed_data_dmi) [root@133 systeminformation]# python sysinformation.py #system information是一個只有一個元素的大的列表 {'ip': ('br1', '112.65.140.133', 'A4:BA:DB:20:93:23')} ['System Information\n\tManufacturer: Dell Inc.\n\tProduct Name: PowerEdge R710\n\tVersion: Not Specified\n\tSerial Number: 4HBDT2X\n\tUUID: 4C4C4544-0048-4210-8044-B4C04F543258\n\tWake-up Type: Power Switch\n\tSKU Number: Not Specified\n\tFamily: Not Specified\n']
[root@133 systeminformation]# vim sysinformation.py 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 def parseDmi(parsed_data): dic = {} parsed_data = [i for i in parsed_data if i.startswith('System Information')] return parsed_data[0] #把大的列表轉(zhuǎn)為一個字符串 if __name__ == "__main__": data_ip = getIfconfig() parsed_data_ip = parseData(data_ip) print parseIfconfig(parsed_data_ip) data_dmi = getDmi() parsed_data_dmi = parseData(data_dmi) print parseDmi(parsed_data_dmi) [root@133 systeminformation]# python sysinformation.py {'ip': ('br1', '112.65.140.133', 'A4:BA:DB:20:93:23')} System Information #把大的列表轉(zhuǎn)為一個字符串,結(jié)果如下: Manufacturer: Dell Inc. Product Name: PowerEdge R710 Version: Not Specified Serial Number: 4HBDT2X UUID: 4C4C4544-0048-4210-8044-B4C04F543258 Wake-up Type: Power Switch SKU Number: Not Specified Family: Not Specified
def parseDmi(parsed_data): dic = {} parsed_data = [i for i in parsed_data if i.startswith('System Information')] return parsed_data[0].split('\n')#使用\n作為切割符號,結(jié)果是列表,有很多元素,而不是一個元素 [root@133 systeminformation]# python sysinformation.py {'ip': ('br1', '112.65.140.133', 'A4:BA:DB:20:93:23')} ['System Information', '\tManufacturer: Dell Inc.', '\tProduct Name: PowerEdge R710', '\tVersion: Not Specified', '\tSerial Number: 4HBDT2X', '\tUUID: 4C4C4544-0048-4210-8044-B4C04F543258', '\tWake-up Type: Power Switch', '\tSKU Number: Not Specified', '\tFamily: Not Specified', ''] [root@133 systeminformation]# #使用[1:]丟棄列表第一個元素:'System Information' def parseDmi(parsed_data): dic = {} parsed_data = [i for i in parsed_data if i.startswith('System Information')] return parsed_data[0].split('\n')[1:] [root@133 systeminformation]# python sysinformation.py {'ip': ('br1', '112.65.140.133', 'A4:BA:DB:20:93:23')} ['\tManufacturer: Dell Inc.', '\tProduct Name: PowerEdge R710', '\tVersion: Not Specified', '\tSerial Number: 4HBDT2X', '\tUUID: 4C4C4544-0048-4210-8044-B4C04F543258', '\tWake-up Type: Power Switch', '\tSKU Number: Not Specified', '\tFamily: Not Specified', ''] #使用if i去掉最后一個空元素‘ ’,使用dict函數(shù)將列表元素轉(zhuǎn)換為字典 def parseDmi(parsed_data): dic = {} parsed_data = [i for i in parsed_data if i.startswith('System Information')] parsed_data = [i for i in parsed_data[0].split('\n')[1:] if i] return dict ([i.strip().split(':') for i in parsed_data]) [root@133 systeminformation]# python sysinformation.py #輸出全為字典元素,key-value,然后把指定的key賦值給需要的key即可輸出value {'ip': ('br1', '112.65.140.133', 'A4:BA:DB:20:93:23')} {'SKU Number': ' Not Specified', 'UUID': ' 4C4C4544-0048-4210-8044-B4C04F543258', 'Family': ' Not Specified', 'Serial Number': ' 4HBDT2X', 'Version': ' Not Specified', 'Product Name': ' PowerEdge R710', 'Wake-up Type': ' Power Switch', 'Manufacturer': ' Dell Inc.'} #dic ['vender'] = dmi_dic['Manufacturer'] 這是把dmi_dic字典key和value Manufacturer:Dell Inc. 賦值給字典給dic字典的 vender,其它同理,最后輸出dic,即輸出三個key value的字典dic內(nèi)容 def parseDmi(parsed_data): dic = {} parsed_data = [i for i in parsed_data if i.startswith('System Information')] parsed_data = [i for i in parsed_data[0].split('\n')[1:] if i] dmi_dic = dict ([i.strip().split(':') for i in parsed_data]) dic ['vender'] = dmi_dic['Manufacturer'] dic ['product'] = dmi_dic['Product Name'] dic ['sn'] = dmi_dic['Serial Number'] return dic [root@133 systeminformation]# python sysinformation.py {'ip': ('br1', '112.65.140.133', 'A4:BA:DB:20:93:23')} {'vender': ' Dell Inc.', 'product': ' PowerEdge R710', 'sn': ' 4HBDT2X'} #發(fā)現(xiàn)value前面有空格,使用strip()刪除空格 def parseDmi(parsed_data): dic = {} parsed_data = [i for i in parsed_data if i.startswith('System Information')] parsed_data = [i for i in parsed_data[0].split('\n')[1:] if i] dmi_dic = dict ([i.strip().split(':') for i in parsed_data]) dic ['vender'] = dmi_dic['Manufacturer'].strip() dic ['product'] = dmi_dic['Product Name'].strip() dic ['sn'] = dmi_dic['Serial Number'].strip() return dic [root@133 systeminformation]# python sysinformation.py {'ip': ('br1', '112.65.140.133', 'A4:BA:DB:20:93:23')} {'vender': 'Dell Inc.', 'product': 'PowerEdge R710', 'sn': '4HBDT2X'}
下面是收集到所有的信息,以key value的形式保存在字典中
cat sysinformation.py #!/usr/bin/env python 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 def parseDmi(parsed_data): dic = {} parsed_data = [i for i in parsed_data if i.startswith('System Information')] parsed_data = [i for i in parsed_data[0].split('\n')[1:] if i] dmi_dic = dict ([i.strip().split(':') for i in parsed_data]) dic ['vender'] = dmi_dic['Manufacturer'].strip() dic ['product'] = dmi_dic['Product Name'].strip() dic ['sn'] = dmi_dic['Serial Number'].strip() return dic #獲取主機名信息:/etc/sysconfig/network def getHostname(f): with open(f) as fd: for line in fd: if line.startswith('HOSTNAME'): hostname = line.split('=')[1].strip() break return {'hostname':hostname} #獲取OS版本信息:打開文件/etc/issue def getOSver(f): with open(f) as fd: for line in fd: osver = line.strip() break return {'osver':osver} #獲取cpu信息:/proc/cpuinfo def getCpu(f): num = 0 with open(f) as fd: for line in fd: if line.startswith('processor'): num +=1 if line.startswith('model name'): cpu_model = line.split(':')[1].split() cpu_model = cpu_model[0] + ' '+cpu_model[-1] return {'cpu_num':num, 'cpu_model':cpu_model} #獲取內(nèi)存信息:/proc/meminfo def getMemory(f): with open(f) as fd: for line in fd: if line.startswith('MemTotal'): mem = int(line.split()[1].strip()) break mem = "%s" % int(mem/1024.0)+'M' return {'memory':mem} if __name__ == "__main__": dic = {} data_ip = getIfconfig() parsed_data_ip = parseData(data_ip) ip = parseIfconfig(parsed_data_ip) data_dmi = getDmi() parsed_data_dmi = parseData(data_dmi) dmi = parseDmi(parsed_data_dmi) hostname = getHostname('/etc/sysconfig/network') osver = getOSver('/etc/issue') cpu = getCpu('/proc/cpuinfo') mem = getMemory('/proc/meminfo') dic.update(ip) dic.update(dmi) dic.update(hostname) dic.update(cpu) dic.update(mem) dic.update(osver) print dic [root@133 systeminformation]# python sysinformation.py {'product': 'PowerEdge R710', 'osver': 'CentOS release 6.7 (Final)', 'sn': '4HBDT2X', 'memory': '19976M', 'cpu_num': 8, 'ip': ('br1', '112.65.140.133', 'A4:BA:DB:20:93:23'), 'vender': 'Dell Inc.', 'hostname': '133', 'cpu_model': 'Intel(R) 2.00GHz'}
注:
mem = "%s" % int(mem/1024.0)+'M'
%表示格式化字符串的格式符號。
%s,表示字符串
%d,表示整數(shù)的字符串
%x,表示16進制的字符串
%f,表示浮點數(shù)字符串
第二個%表示要格式化的對象。
這種方法可以進行類型轉(zhuǎn)換。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
文章名稱:收集主機信息-創(chuàng)新互聯(lián)
文章URL:http://aaarwkj.com/article0/iejio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、網(wǎng)站營銷、企業(yè)建站、虛擬主機、域名注冊、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容