paramiko获取主机信息

  1 import re
  2 import paramiko
  3 
  4 host="192.168.4.88"
  5 user = "root"
  6 password = "123456"
  7 
  8 class GetLinuxMessage:
  9 #登录远程Linux系统
 10     def session(self, host, port, username, password=password):
 11 
 12         try:
 13             ssh = paramiko.SSHClient()
 14             ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 15             ssh.connect(host, int(port), username, password)
 16             print("Login %s is successful" % host)
 17             return ssh
 18         except Exception as e:
 19             print(e.message)
 20 
 21 
 22     #获取Linux主机名/root/桌面/开发/h5
 23     def get_hostname(self, host, port=22, username=user, password=password):
 24         cmd_hostname = "hostname"
 25         client = self.session(host, port, username, password)
 26         stdin, stdout, stderr = client.exec_command(cmd_hostname)
 27         hostname = stdout.read().decode(encoding='utf-8')
 28         return hostname
 29 
 30     #获取Linux网络ipv4信息
 31     def get_ifconfig(self, host, port=22, username=user, password=password):
 32         client = self.session(host, port, username, password)
 33         stdin, stdout, stderr = client.exec_command("ifconfig eth0")
 34         data = stdout.read().decode(encoding='utf-8')
 35         print(data)
 36         #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]))')
 37         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]))')
 38         match = ret.search(data).group()
 39         return match
 40 
 41     #获取Linux系统版本信息
 42     def get_version(self, host, port=22, username=user, password=password):
 43 
 44         client = self.session(host, port, username, password)
 45         stdin, stdout, stderr = client.exec_command("cat /etc/redhat-release")
 46         data = stdout.read().decode(encoding='utf-8')
 47         return str(data)
 48 
 49     #获取Linux系统CPU信息
 50     def get_cpu(self, host, port=22, username=user, password=password):
 51 
 52         cpunum = 0
 53         processor = 0
 54         client = self.session(host, port, username, password)
 55         stdin, stdout, stderr = client.exec_command("cat /proc/cpuinfo")
 56         cpuinfo = stdout.readlines()
 57         #with stdout.read() as cpuinfo:
 58         for i in cpuinfo:
 59             if i.startswith('physical id'):
 60                 cpunum = i.split(":")[1]
 61             if i.startswith('processor'):
 62                 processor = processor + 1
 63             if i.startswith('model name'):
 64                 cpumode = i.split(":")[1]
 65         return int(cpunum)+1, processor,cpumode
 66 
 67     #获取Linux系统memory信息
 68     def get_memory(self, host, port=22, username=user, password=password):
 69 
 70         client = self.session(host, port, username, password)
 71         stdin, stdout, stderr = client.exec_command("cat /proc/meminfo")
 72         meminfo = stdout.readlines()
 73         #with open('/proc/meminfo') as meminfo:
 74         for i in meminfo:
 75             if i.startswith('MemTotal'):
 76                 memory = int(i.split()[1].strip())
 77                 memory = '%.f' %(memory / 1024.0) + 'MB'
 78             else:
 79                 pass
 80         return memory
 81 
 82     #获取Linux系统网卡信息
 83     def get_ethernet(self, host, port=22, username=user, password=password):
 84 
 85         client = self.session(host, port, username, password)
 86         stdin, stdout, stderr = client.exec_command("lspci")
 87         data = stdout.read().decode(encoding='utf8')
 88         ret = re.compile('Eth[^\d].*')
 89         eth = ret.search(data).group()
 90         return eth
 91 
 92 
 93 if __name__ == '__main__':
 94 
 95     # host = input("please input the hostname: ")
 96     result = GetLinuxMessage()
 97     result1 = result.get_hostname(host)
 98     print ('主机名:%s' %result1)
 99     result2 = result.get_ifconfig(host)
100     print ('主机IP:%s' %result2)
101     result3 = result.get_version(host)
102     print ('版本信息:%s' %result3)
103     result4,result5,result6 = result.get_cpu(host)
104     print ('物理CPU数量:%s' %result4)
105     print ('逻辑CPU数量:%s' %result5)
106     print ('物理CPU型号:%s' %result6)
107     result7 = result.get_memory(host)
108     print ('物理内存:%s' %result7)
109     result8 = result.get_ethernet(host)
110     print ('网卡型号:%s' %result8)

 

import re
import paramiko

host="192.168.4.88"
user = "root"
password = "123456"

class GetLinuxMessage:
#登录远程Linux系统
def session(self, host, port, username, password=password):

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


#获取Linux主机名/root/桌面/开发/h5
def get_hostname(self, host, port=22, username=user, password=password):
cmd_hostname = "hostname"
client = self.session(host, port, username, password)
stdin, stdout, stderr = client.exec_command(cmd_hostname)
hostname = stdout.read().decode(encoding='utf-8')
return hostname

#获取Linux网络ipv4信息
def get_ifconfig(self, host, port=22, username=user, password=password):
client = self.session(host, port, username, password)
stdin, stdout, stderr = client.exec_command("ifconfig eth0")
data = stdout.read().decode(encoding='utf-8')
print(data)
#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(self, host, port=22, username=user, password=password):

client = self.session(host, port, username, password)
stdin, stdout, stderr = client.exec_command("cat /etc/redhat-release")
data = stdout.read().decode(encoding='utf-8')
return str(data)

#获取Linux系统CPU信息
def get_cpu(self, host, port=22, username=user, password=password):

cpunum = 0
processor = 0
client = self.session(host, port, username, password)
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(self, host, port=22, username=user, password=password):

client = self.session(host, port, username, password)
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(self, host, port=22, username=user, password=password):

client = self.session(host, port, username, password)
stdin, stdout, stderr = client.exec_command("lspci")
data = stdout.read().decode(encoding='utf8')
ret = re.compile('Eth[^\d].*')
eth = ret.search(data).group()
return eth


if __name__ == '__main__':

# host = input("please input the hostname: ")
result = GetLinuxMessage()
result1 = result.get_hostname(host)
print ('主机名:%s' %result1)
result2 = result.get_ifconfig(host)
print ('主机IP%s' %result2)
result3 = result.get_version(host)
print ('版本信息:%s' %result3)
result4,result5,result6 = result.get_cpu(host)
print ('物理CPU数量:%s' %result4)
print ('逻辑CPU数量:%s' %result5)
print ('物理CPU型号:%s' %result6)
result7 = result.get_memory(host)
print ('物理内存:%s' %result7)
result8 = result.get_ethernet(host)
print ('网卡型号:%s' %result8)
posted @ 2019-03-29 09:21  Ray_chen  阅读(843)  评论(0编辑  收藏  举报