Python implementation to obtain basic information of Linux operating system
The information obtained is as follows:
Hostnam
System version
System kernel version
Total memory
CPU manufacturer
Total number of cores in CPU
Server manufacturer
Server serial number
IP,MAC and name information of each network card
The implementation code is as follows:
#! / usr/bin/python#coding:utf8from subprocess import Popen, PIPEimport re# gets the hostname, or you can use the uname-n command to 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 operating system 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 CPU Then the statistics of the CPU model is the last model, and this situation 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 # to call this function requires two variables to receive parameters # get memory related information def meminfo (): with open ("/ proc/meminfo") as meminfo: for i in meminfo: if i.startswith ("MemTotal"): 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 # calls this function using two variables to receive parameter # to get network card information Including the name of the network card IP address, MAC address def ipaddrinfo (): # defines the storage format, with the network card name as key,mac address and ip address as a list This list is the value def add (dic, key, value) of the network card name: 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] 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"% ("hostname", 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 manufacturer", cpumode)), print ("% s:\ t% s"% ("Total CPU cores") Len (cpunumber)) print ("% s:\ t% s"% ("server manufacturer", manufacturer)), print ("% s:\ t% s"% ("server serial number", serialnumber)), for x in ipinfo: y = ipinfo.get (x) ip = y [1] mac = y [0] print ("% s% s:\ t% s\ t% s"% ("network card") X, ip, mac))
The output is as follows:
There is no problem with the above results tested by CentOS 7 and CentOS 6, and some IO errors may occur when using other systems.