PYTHON获取远程LINUX主机的一些信息

[root@centos7s python]# cat linuxs.py 
#!/usr/bin/python
#coding:utf-8




from subprocess import Popen,PIPE
import re
import os,sys
import paramiko
reload(sys)
sys.setdefaultencoding('utf-8')
def session(host,uname,pwd):


    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(host,username=uname, password=pwd)
        print "Login %s is successful" % host
        return ssh
    except Exception as e:
        print e.message




def get_hostname(host,uname,pwd):
    cmd_hostname = "hostname"
    client = session(host,uname,pwd)
    stdin,stdout,stderr=client.exec_command(cmd_hostname)
    hostname = stdout.read()
    return hostname




        #获取Linux网络ipv4信息
def get_ifconfig(host,uname,pwd):
    client = session(host,uname,pwd)
    stdin, stdout, stderr = client.exec_command("ifconfig")
    data = stdout.read()
    #ret = re.compile('((?:1[0-9][0-9]\.)|(?:25[0-5]\.)|(?:2[0-4][0-9]\.)|(?:[1-9][0-9]\.)|(?:[0-9]\.)){3}((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))')
    ret = re.compile('(?:19[0-9]\.)((?:1[0-9][0-9]\.)|(?:25[0-5]\.)|(?:2[0-4][0-9]\.)|(?:[1-9][0-9]\.)|(?:[0-9]\.)){2}((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))')
    match = ret.search(data).group()
    return match


        #获取Linux系统版本信息
def get_version(host,uname,pwd):


    client = session(host,uname,pwd)
    stdin, stdout, stderr = client.exec_command("cat /etc/redhat-release")
    data = stdout.read()
    return data
#获取Linux系统CPU信息
def get_cpu(host,uname,pwd):


        cpunum = 0
        processor = 0
        client = session(host,uname,pwd)
        stdin, stdout, stderr = client.exec_command("cat /proc/cpuinfo")
        cpuinfo = stdout.readlines()
#with stdout.read() as cpuinfo:
        for i in cpuinfo:
                if i.startswith('physical id'):
                        cpunum = i.split(":")[1]
                if i.startswith('processor'):
                        processor = processor + 1
                if i.startswith('model name'):
                        cpumode = i.split(":")[1]
        return int(cpunum)+1, processor,cpumode


        #获取Linux系统memory信息
def get_memory(host,uname,pwd):


    client = session(host,uname,pwd)
    stdin, stdout, stderr = client.exec_command("cat /proc/meminfo")
    meminfo = stdout.readlines()
#with open('/proc/meminfo') as meminfo:
    for i in meminfo:
        if i.startswith('MemTotal'):
            memory = int(i.split()[1].strip())
            memory = '%.f' %(memory / 1024.0) + 'MB'
        else:
                pass
        return memory


        #获取Linux系统网卡信息
def get_ethernet(host,uname,pwd):


    client = session(host,uname,pwd)
    stdin, stdout, stderr = client.exec_command("lspci")
    data = stdout.read()
    ret = re.compile('Eth[^\d].*')
    eth = ret.search(data).group()
    return eth




if __name__ == '__main__':
        host=sys.argv[1]
        uname=sys.argv[2]
        pwd=sys.argv[3]
        port=22
        #host = raw_input("please input the hostname: ")
        #result = GetLinuxMessage()
        result1 = get_hostname(host,uname,pwd)
        print ('主机名:%s' %result1)
        result2 = get_ifconfig(host,uname,pwd)
        print ('主机IP:%s' %result2)
        result3 = get_version(host,uname,pwd)
        print ('版本信息:%s' %result3)
        result4,result5,result6 = get_cpu(host,uname,pwd)
        print ('物理CPU数量:%s' %result4)
        print ('逻辑CPU数量:%s' %result5)
        print ('物理CPU型号:%s' %result6)
        result7 = get_memory(host,uname,pwd)
        print ('物理内存:%s' %result7)
        result8 = get_ethernet(host,uname,pwd)

        print ('网卡型号:%s' %result8)


执行结果:

[root@centos7s python]# ./linuxs.py '193.168.120.77' 'root' 'pwd'
Login 193.168.120.77 is successful
主机名:cons7s


Login 193.168.120.77 is successful
主机IP:193.168.120.77
Login 193.168.120.77 is successful
版本信息:CentOS Linux release 7.3.1611 (Core) 


Login 193.168.120.77 is successful
物理CPU数量:1
逻辑CPU数量:2
物理CPU型号: Intel(R) Xeon(R) CPU            5150  @ 2.66GHz


Login 193.168.120.77 is successful
物理内存:1832MB
Login 193.168.120.77 is successful
网卡型号:Ethernet controller: Intel Corporation 82545EM Gigabit Ethernet Controller (Copper) (rev 01)

你可能感兴趣的:(PYTHON获取远程LINUX主机的一些信息)