python 获取centos主机名,ip 逻辑cpu个数

def get_ip(ifname):
    "取网卡的ip"
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', bytes(ifname[:15],'utf-8')))[20:24])

def get_hostname():
    command = "hostname"
    hostname = os.popen(command).read()
    print(hostname.replace("\n", ""))
    return hostname


def getCpuCount():
    "获取逻辑cpu"
    with open('/proc/cpuinfo') as  f:
        cpu_cpunt = 0
        for line in f:
            line = line.lower().lstrip()
            if line.startswith('processor'):
                cpu_cpunt = cpu_cpunt + 1
    return cpu_cpunt

你可能感兴趣的:(python)