paramiko获取主机信息
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获取主机信息的更多相关文章
- Linux 网络编程基础(2)-- 获取主机信息
前一篇已经介绍了最基本的网络数据结构.这篇介绍一下获取主机信息的函数 举个例子,想要通过代码的方式从百度获取当前的时间,怎么做?我们不知道百度的IP地址啊,这代码怎么写?还好,Linux提供了一些AP ...
- PHP通过ZABBIX API获取主机信息 VS 直接从数据库获取主机信息
最近项目需要获取linux主机的一些信息,如CPU使用率,内存使用情况等.由于我们本身就装了zabbix系统,所以我只用知道如何获取信息即可,总结有两种方法可以获取. 一.通过ZABBIX API获取 ...
- 获取主机信息,网络信息AIP,getsockname,getpeername,getservbyname,getservbyport,inet_ntop,inet_pton
获取主机信息 1.ip地址转换,主机字节序 <---> 网络字节序 #include <arpa/inet.h> int inet_pton(int af, const cha ...
- C#获取主机信息
获取主机信息 最近需要做一个配合集控系统收集各个终端设备的一些信息,大致需要收集终端设备的硬件信息,CPU.内存以及硬盘使用率等信息.网上查看了一番,使用WMI来获取这些信息是最方便的.实现代码如下: ...
- 【Java实用工具】——使用oshi获取主机信息
最近在筹划做一个监控系统.其中就要获取主机信息,其中遇到一些问题.在此做个记录,以便以后查阅. 在该监控系统中,想要做到主机的CPU.内存.磁盘.网络.线程.JVM内存.JVM GC 等维度的监控,J ...
- python 调用zabbix api实现查询主机信息,输出所有主机ip
之前发现搜索出来的主机调用zabbix api信息都不是那么明确,后来通过zabbix官方文档,查到想要的api信息,随后写一篇自己这次项目中用到的api. #!/usr/bin/env python ...
- 02.将SDK获取到的ECS主机信息入库
1.通过调用阿里SDK,将获取到的ECS信息存入数据库,如果不知道SDK怎么使用,查看:01.阿里云SDK调用,获取ESC主机详细信息 2.import aliSDK应用的是01.阿里云SDK调用,获 ...
- 使用gethostname()函数和gethostbyname()函数获取主机相关信息
gethostname() : 返回本地主机的标准主机名. 原型如下: #include <unistd.h> int gethostname(char *name, size_t len ...
- 主机性能监控之wmi 获取磁盘信息
标 题: 主机性能监控之wmi 获取磁盘信息作 者: itdef链 接: http://www.cnblogs.com/itdef/p/3990541.html 欢迎转帖 请保持文本完整并注明出处 仅 ...
随机推荐
- 题解 CF437C
基本思路---贪心 既然要求最小代价,当用一定顺序删除时代价一定最小,不难发现,每次都删去x,y中最小的,最后的总代价业一定最小! 因此就可以写出下面的简单的代码 代码 #include<ios ...
- springboot项目在IDEA根据不同的开发人员读取不同的配置文件
IDEA启动项目打开项目的配置文件,修改Program argument为--spring.profiles.active=developerName启动项目,即可 命令行方式启动项目 java -j ...
- css 学习笔记 常用到的知识
做 loading 居中 剧中通常就是 top left 50% 再调一下自己就可以了 关键是要有 width height 遇到一些base on content 决定 height 的情况一般上有 ...
- 情感交流篇:HTML页面如何与后端联系
通过ajax get方法: 基本格式:$.get("后台一般处理程序文件路径",{传值,格式为 KEY:VULES},function(后台返回值){接到后台数据后处理}); $ ...
- 奇妙的算法【11】LeetCode-专属算法面试题汇总
这个是LeetCode上面的编程训练专项页面,地址:https://leetcode-cn.com/explore/interview/card/top-interview-quesitons-in- ...
- [NOIP2018模拟赛10.23]发呆报告
闲扯 考场看了眼题目感觉很难,一个小时敲完了所有暴力...嗯然后就在那里发呆什么事也没做 T3考场上把数据结构想了个遍都不会完成1操作,现在看这种思路其实之前也接触过... 比较玄学的一件事情就是T1 ...
- Django rest-framework框架-组件之分页
分页: a. 分页,看第n页, 每页显示n条数据 from rest_framework import serializers from api import models from rest_fra ...
- K2 BPM_北汽新能源业务流程管理信息系统建设思考_全球领先的工作流引擎
本文由CIO发展中心根据北汽新能源流程与IT总监刘伟霞在“亦庄CIO数字化转型探索——CIO发展中心亦庄分舵2019夏季论坛”活动中演讲整理. 在“亦庄CIO数字化转型探索——CIO发展中心亦庄分舵2 ...
- 使用私有api实现自己的iphone桌面,并根据app的使用次数对app排序
使用<iphone SprintBoard部分私有API总结>中提到的api,除了能对app运行次数进行监控以外,还可以实现自己的iphone桌面,并根据app 的使用次数对app图标进行 ...
- flex布局实战
1.实现盒子的水平垂直居中 .parent{ width:200px; height:200px; display:flex; align-items: center; justify-content ...