Python獲取系統信息(慢慢補充)


獲取OS信息:

1. os = " ".join(platform.linux_distribution())
2. os = subprocess.call(['cat', '/etc/redhat-release'])

獲取系統信息

import subprocess

def get_sysinfo()
    sys = subprocess.Popen(['dmidecode','-t','system'], stdout=subprocess.PIPE)
    sys_list = sys.stdout.readlines()
    sys_dict = {}
    for line in sys_list:
        if "Manufacturer" in line.decode():
            sys_dict["manufacturers"] = line.decode().split(':')[1].strip()
        if "Product" in line.decode():
            sys_dict["server_type"] = line.decode().split(':')[1].strip()
        if "Serial" in line.decode():
            sys_dict["sn"] = line.decode().split(':')[1].strip()
        if "UUID" in line.decode():
            sys_dict["uuid"] = line.decode().split(':')[1].strip()
    return sys_dict

獲取主機名

import socket
hostname = socket.gethostname()

獲取CPU信息:

獲取物理CPU個數

1. os.cpu_count()
2. psutil.cpu_count()

獲取CPU型號

with open('/proc/cpuinfo') as f:
    for line in f:
        if line.startswith('model name'):
            server_cpu = line.split(":")[1].strip()      

自定義時間戳

>>> import time
>>> b = time.time()
>>> b
1521680652.923847
>>> c = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(b))
>>> c
'2018-03-22 09:04:12'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM