Get Linux system host information using Python collection

Source: Internet
Author: User

Use Python code to collect system information for hosts, mainly: hostname, IP, system version, server manufacturer, model, serial number, CPU information, memory and other system information.

#!/usr/bin/env python#Encoding:utf-8" "Collect host Information: host name, IP, system version, server manufacturer, model, serial number, CPU information, memory information" " fromSubprocessImportPopen, PIPEImportOs,sys" "get the output of the ifconfig command" "defgetifconfig (): P= Popen (['ifconfig'], stdout =PIPE) Data=P.stdout.read ()returnData" "get the output of the Dmidecode command" "defGetdmi (): P= Popen (['Dmidecode'], stdout =PIPE) Data=P.stdout.read ()returnData" "returns a list of paragraphs based on a blank line paragraph" "defparsedata (data): Parsed_data=[] New_line="'Data= [I forIinchData.split ('\ n')ifi] forLineinchData:ifLine[0].strip (): Parsed_data.append (new_line) new_line= line +'\ n'        Else: New_line+ = line +'\ n'parsed_data.append (new_line)return[I forIinchParsed_dataifi]" "Analyze ifconfig IP information for each NIC based on the input paragraph data" "defParseifconfig (parsed_data): DiC={} parsed_data= [I forIinchParsed_dataif  notI.startswith ('Lo')]     forLinesinchparsed_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]         Breakdic['IP'] =ipaddrreturnDiC" "analysis of specified parameters based on the DMI paragraph data entered" "defParsedmi (parsed_data): DiC={} parsed_data= [I forIinchParsed_dataifI.startswith ('System Information')] Parsed_data= [I forIinchParsed_data[0].split ('\ n') [1:]ifi] dmi_dic= Dict ([I.strip (). Split (':') forIinchParsed_data]) dic['vender'] = dmi_dic['manufacturer'].strip () dic['Product'] = dmi_dic['Product Name'].strip () dic['SN'] = dmi_dic['Serial Number'].strip ()returnDiC" "get the Linux system host name" "defgethostname (): With open ('/etc/sysconfig/network') as FD: forLineinchFD:ifLine.startswith ('HOSTNAME'): hostname= Line.split ('=') [1].strip () Break    return{'hostname': hostname}" "get version information for a Linux system" "defgetosversion (): With open ('/etc/issue') as FD: forLineinchFd:osver=Line.strip () Break    return{'Osver': Osver}" "Gets the model number of the CPU and the cores of the CPU" "defgetcpu (): Num=0 with open ('/proc/cpuinfo') as FD: forLineinchFD:ifLine.startswith ('Processor'): Num+ = 1ifLine.startswith ('model name'): Cpu_model= Line.split (':') [1].strip (). Split () Cpu_model= Cpu_model[0] +' '+ cpu_model[2] +' '+ cpu_model[-1]    return{'Cpu_num': Num,'Cpu_model': Cpu_model}" "get the total physical memory of a Linux system" "defgetmemory (): With open ('/proc/meminfo') as FD: forLineinchFD:ifLine.startswith ('Memtotal'): Mem= Int (Line.split () [1].strip ()) BreakMem='%.F'% (mem/1024.0) +'MB'    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 () osver=getosversion () CPU=getcpu () mem=getmemory () dic.update (IP) dic.update (DMI) dic.update (hostname) dic.update (osver) dic.update (CPU) Dic.update (MEM)" "all data information to be obtained and aligned in simple format" "     forKvinchDic.items ():Print '%-10s:%s'%(k, v)

Experimental test results:

 product:vmware Virtual platformosver:centos release  6.4   (Final) sn:vmware -56  4d B4 6c 05  e5 20  dc-c6 49  0c E1 e0 18  1c 75  memory:  1870   mbcpu _num:  2  IP:  192.168 . 0.8  vender:vmware, Inc.  hostname  : Vipcpu_model:intel (R) i7  -4710mq 2 . 50GHz 

Get Linux system host information using Python collection

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.