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 欢迎转帖 请保持文本完整并注明出处 仅 ...
随机推荐
- ElasticSearch入门-基本概念介绍以及安装
Elasticsearch基本概念 Elasticsearch是基于Lucene的全文检索库,本质也是存储数据,很多概念与传统关系型数据库类似. 传统关系型数据库与Elasticsearch进行概念对 ...
- Centos7.3安装,并设置网络和防火墙
下载centos7.3安装ISO 最小化安装,随后打通网络,完成网络设置.安装VIM,关闭firewalld防火墙,打开iptables防火墙 重启, vim /etc/sysconfig/netwo ...
- 如何将Linux的工程原封不动地移植到Windows上面
习惯在Linux下进行开发.但是由于工作需要,不得不与其他使用Windows的项目组同事对接,同事要求我给出可用的程序,而我只有基于makefile的传统工程. 改动到VS工程上发现一部分头文件在Wi ...
- docker相关--dockerd日志设置
背景 线上容器dockerd的后台程序打印了超过几十G的日志 Docker daemon日志的位置: Docker daemon日志的位置,根据系统不同各不相同. Ubuntu - /var/log/ ...
- AtCoder Grand Contest 040 B - Two Contests
传送门 一看就感觉很贪心 考虑左端点最右的区间 $p$ 和右端点最左的区间 $q$ 如果 $p,q$ 属于同一个集合(设为 $S$,另一个集合设为 $T$),那么其他的区间不管是不是在 $S$ 都不会 ...
- maftools | 从头开始绘制发表级oncoplot(瀑布图)
本文首发于微信公众号 **“ 生信补给站 ”** ,期待您的关注!!! 原文链接:https://mp.weixin.qq.com/s/G-0PtaoO6bYuhx_D_Rlrlw 对于组学数据的分析 ...
- 关于spring中配置文件路径的那些事儿
在项目中我们经常会需要读一些配置文件来获取配置信息,然而对于这些配置文件在项目中存放的位置以及获取这些配置文件的存放路径却经常搞不清楚,自己研究了一下,记录下来以备后用. 测试代码如下 package ...
- 奇妙的算法【11】LeetCode-专属算法面试题汇总
这个是LeetCode上面的编程训练专项页面,地址:https://leetcode-cn.com/explore/interview/card/top-interview-quesitons-in- ...
- MySql8.0 安装重要的两步。
1.去官网下载mysql社区版 windows安装包. 2.在安装包 安装的过程中,有一步就是启动mysql 会失败: 然后修改服务后,再次回到安装界面点击:execute,就会成功了. 先去配置一下 ...
- epoll、mysql概念及简单操作
epoll 程序阻塞的过程 假设我们目前运行了三个进程A B C ,如果他们都在处于运行态,那就会被加到一个运行队列中 进程A正在运行socket程序 在linux中有句话,万物皆文件,socket对 ...