Python implements basic information for Linux operating systems

Source: Internet
Author: User

Get the following information:

Host Name

System version

System Kernel Version

Total Memory

CPU Manufacturers

CPU Total Core Number

Server Manufacturers

Server serial Number

Each NIC Ip,mac and Nic name information


The implementation code is as follows:


#!/usr/bin/python#coding:utf8from subprocess import popen, pipeimport re# Gets the host name, or you can use the  uname -n  command Get Def hostname ():     hostname = popen (["hostname "], stdout=pipe)     hostname = hostname.stdout.read ()      return hostname# get operating system version def osversion ():     with open ("/etc/ Redhat-release ")  as f:        osversion = f.read ()     return osversion# get OS kernel version def oscoreversion ():     Oscoreversion = popen (["Uname",  "-r"], stdout=pipe)     oscoreversion  = oscoreversion.stdout.read ()     return oscoreversion# get CPU-related information, If there are many different CPUs, then the CPU model is counted as the last model, which is rare def cpuinfo ():    corenumber = []     with open ("/proc/cPuinfo ")  as cpuinfo:        for i in cpuinfo:             if i.startswith ("Processor"):                  Corenumber.append (i)             if  I.startswith ("Model name"):                 cpumode = i.split (":") [1]    return corenumber,  cpumode# Call this function requires two variables to receive parameters      #获取内存相关信息def  meminfo ():     with  open ("/proc/meminfo")  as meminfo:        for i  in meminfo:            if  I.startswith ("Memtotal"):      &NBSp;          totalmem = i.split (":") [1]     return totalmem# Get server hardware related Information def biosinfo ():     biosinfo =  popen (["Dmidecode",  "-T",  "System"], stdout=pipe)     biosinfo =  biosinfo.stdout.readlines ()         for i in  biosinfo:        if  "Manufacturer"  in i:             manufacturer = i.split (":") [1]         if  "Serial number"  in i:             serialnumber = i.split (":") [1]     return manufacturer, serialnumber# Call this function requires the use of two variables to receive parameters      #获取网卡信息, including network card name, IP address, MAC address Def ipaddrinfo ():     #定义存储格式 to a list of network cards named KEY,MAC addresses and IP addresses, and this list is the Value    def add for this NIC name (DIC,  key, value):          dic.setdefault (key, [ ] ). Append (value)         ipinfo = popen (["IP",  "addr"],  stdout=pipe)     ipinfo = ipinfo.stdout.readlines ()          dict1 = {}    for i in ipinfo:         if re.search (R "^\d",  i):             devname = i.split (": ") [1]             continue        if  re.findall ("Ether",  i):             Devmac = i.split () [1]&NBSp;           add (DICT1, DEVNAME, DEVMAC)             continue         if re.findall ("global",  i):             devip = i.split () [1]             add (DICT1, DEVNAME, DEVIP)              continue    return dict1    if __name__ ==   "__main__":     hostname = hostname ()     osversion  = osversion ()     oscoreversion = oscoreversion ()      totalmem = meminfo ()     cpunumber, cpumode = cpuinfo ()      manUfacturer, serialnumber = biosinfo ()     ipinfo = ipaddrinfo ()     print ("%s:\t\t %s"  % ("host name",  hostname)),     print ("% s:\t %s " % (" System version ",  osversion)),     print ("%s:\t %s " % (" System kernel version ",  oscoreversion)),     print ("%s:\t %s"  % ("Total Memory",  totalmem)),     print ("%s:\t%s"  % ("CPU maker",  cpumode),     print ("%s:\t %s")  % ("Total CPU Cores",  len (Cpunumber)))     print ("%s:\t%s"  % ("Server Builder",  Manufacturer)),     print ("%s:\t%s"  % ("Server serial number",  serialnumber)),     for x in ipinfo:        y = ipinfo.get ( x)         ip = y[1]         mac = y[0]&nBsp;       print ("%s%s:\t %s\t%s"  % ("Nic", x, ip,  MAC))



The output results are as follows:


650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/99/74/wKioL1lIma_wjaIkAACVS9P_wY0954.png-wh_500x0-wm_ 3-wmp_4-s_145611385.png "style=" Float:none; "title=" 3pyw7i2s@ ' e8js5rd (v}$$9.png "alt=" Wkiol1lima_wjaikaacvs9p_ Wy0954.png-wh_50 "/>

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/99/74/wKiom1lImbDiZz9lAACsn-kge98419.png-wh_500x0-wm_ 3-wmp_4-s_3630201923.png "style=" Float:none; "title=" OCOXYXQ6) 7kbi6{$4_50bn7.png "alt=" Wkiom1limbdizz9laacsn-kge98419.png-wh_50 "/>


These results are not problematic with CentOS 7 and CentOS 6 testing, and some IO errors may occur with other systems

This article is from the "Blue _ Storm" blog, make sure to keep this source http://270142877.blog.51cto.com/12869137/1940151

Python implements basic information for Linux operating systems

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.