import re
import paramiko host="192.168.4.88"
user = "root"
password = "" 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)
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=, 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=, 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=, 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=, 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(":")[]
if i.startswith('processor'):
processor = processor + 1
if i.startswith('model name'):
cpumode = i.split(":")[]
return int(cpunum)+, processor,cpumode #获取Linux系统memory信息
def get_memory(self, host, port=, 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()[].strip())
memory = '%.f' %(memory / 1024.0) + 'MB'
else:
pass
return memory #获取Linux系统网卡信息
def get_ethernet(self, host, port=, 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)

paramiko获取主机信息的更多相关文章

  1. Linux 网络编程基础(2)-- 获取主机信息

    前一篇已经介绍了最基本的网络数据结构.这篇介绍一下获取主机信息的函数 举个例子,想要通过代码的方式从百度获取当前的时间,怎么做?我们不知道百度的IP地址啊,这代码怎么写?还好,Linux提供了一些AP ...

  2. PHP通过ZABBIX API获取主机信息 VS 直接从数据库获取主机信息

    最近项目需要获取linux主机的一些信息,如CPU使用率,内存使用情况等.由于我们本身就装了zabbix系统,所以我只用知道如何获取信息即可,总结有两种方法可以获取. 一.通过ZABBIX API获取 ...

  3. 获取主机信息,网络信息AIP,getsockname,getpeername,getservbyname,getservbyport,inet_ntop,inet_pton

    获取主机信息 1.ip地址转换,主机字节序 <---> 网络字节序 #include <arpa/inet.h> int inet_pton(int af, const cha ...

  4. C#获取主机信息

    获取主机信息 最近需要做一个配合集控系统收集各个终端设备的一些信息,大致需要收集终端设备的硬件信息,CPU.内存以及硬盘使用率等信息.网上查看了一番,使用WMI来获取这些信息是最方便的.实现代码如下: ...

  5. 【Java实用工具】——使用oshi获取主机信息

    最近在筹划做一个监控系统.其中就要获取主机信息,其中遇到一些问题.在此做个记录,以便以后查阅. 在该监控系统中,想要做到主机的CPU.内存.磁盘.网络.线程.JVM内存.JVM GC 等维度的监控,J ...

  6. python 调用zabbix api实现查询主机信息,输出所有主机ip

    之前发现搜索出来的主机调用zabbix api信息都不是那么明确,后来通过zabbix官方文档,查到想要的api信息,随后写一篇自己这次项目中用到的api. #!/usr/bin/env python ...

  7. 02.将SDK获取到的ECS主机信息入库

    1.通过调用阿里SDK,将获取到的ECS信息存入数据库,如果不知道SDK怎么使用,查看:01.阿里云SDK调用,获取ESC主机详细信息 2.import aliSDK应用的是01.阿里云SDK调用,获 ...

  8. 使用gethostname()函数和gethostbyname()函数获取主机相关信息

    gethostname() : 返回本地主机的标准主机名. 原型如下: #include <unistd.h> int gethostname(char *name, size_t len ...

  9. 主机性能监控之wmi 获取磁盘信息

    标 题: 主机性能监控之wmi 获取磁盘信息作 者: itdef链 接: http://www.cnblogs.com/itdef/p/3990541.html 欢迎转帖 请保持文本完整并注明出处 仅 ...

随机推荐

  1. spring cloud微服务实践二

    在上一篇,我们已经搭建了spring cloud微服务中的注册中心.但只有一个注册中心还远远不够. 接下来我们就来尝试提供服务. 注:这一个系列的开发环境版本为 java1.8, spring boo ...

  2. SAS学习笔记40 SAS程序运行过程

    当我们提交运行一个DATA步程序后,具体发生了什么事情. SAS程序与其他程序一样,在运行时都要经过两个阶段:编译(Compilation).执行(Execution) 程序首先经过编译阶段,该阶段主 ...

  3. Hibernate之关联关系(一对多)

    今日分享hibernate框架的简单关联关系 一:关联关系简介 1.1 什么是关联关系 关联指的是类之间的引用关系.如果类A与类B关联,那么被引用的类B将被定义为类A的属性. 例如: class B{ ...

  4. Spring (3)框架

    Spring第三天笔记 今日内容 Spring的核心之一 -  AOP思想 (1) 代理模式- 动态代理 ① JDK的动态代理 (Java官方) ② CGLIB 第三方代理 (2) AOP思想在Spr ...

  5. 微信小程序页面滚动到指定位置

    页面上有一个元素或者组件,id 为 comment 则: var me = this; var query = wx.createSelectorQuery().in(me); query.selec ...

  6. (二十八)jsp之EL表达式

    一.EL表达式简介 EL 全名为Expression Language.EL主要作用: 1.获取数据 EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的web域 中检索java对象.获取数 ...

  7. .net Core CLR

    .net Core CLR是开源的.大部分文件是C++写成.这样他就可以编译后再不同的平台运行. https://github.com/dotnet/coreclr

  8. 【原创】大叔经验分享(89)docker启动openjdk执行jmap报错

    docker启动openjdk后,可以查看进程 # docker exec -it XXX jps 10 XXX.jar 可见启动的java进程id一直为10,然后可以执行jvm命令,比如 # doc ...

  9. IOC实现-Unity

    .NET中实现IOC有很多方式,比如:Unity.Ninject.Autofac.MEFNinject的实现参考<Pro ASP.NET MVC3.5 FrameWork>下面给出的是Un ...

  10. Echarts配置项详解

    1.图表标题 title: { x: 'left', // 水平安放位置,默认为左对齐,可选为: // 'center' ¦ 'left' ¦ 'right' // ¦ {number}(x坐标,单位 ...