Python读取shell的输出示例

from subprocess import Popen, PIPE

def hostname():
    hostname = Popen(["hostname"], stdout=PIPE)
    hostname = hostname.stdout.read()
    return hostname


def osversion(): 
    with open("/etc/issue") as f: 
        osversion = f.read() 
    return osversion


def oscoreversion(): 
    oscoreversion = Popen(["uname", "-r"], stdout=PIPE) 
    oscoreversion = oscoreversion.stdout.read() 
    return oscoreversion


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 


def meminfo(): 
    with open("/proc/meminfo") as meminfo: 
        for i in meminfo: 
            if i.startswith("MemTotal"): 
                totalmem = i.split(":")[1] 
    return totalmem
上一篇:linux – 何时交换触发或如何计算swap_tendency?


下一篇:awk命令使用