通过paramiko模块在远程主机上执行命令
安装paramiko模块
/usr/local/python36/bin/pip3 install paramiko
1、获取cpu使用率
#!/usr/bin/python
#coding=utf8
#target: get the cpu's used of remote linux system import paramiko def getlinux(ssh):
# get the result of executing command
stdin, stdout, stderr = ssh.exec_command(r"sar 2 3|awk 'END{print (100-$NF)*100}'")
# judge if there is any error
err = stderr.readlines()
if len(err) > 0:
return err
else:
stdout_content = stdout.readlines()
result = stdout_content
if len(result) == 0:
print("there is something wrong when executing sar command")
else:
return round(float(result[0].strip()),2) if __name__ == '__main__':
# create ssh object
ssh = paramiko.SSHClient()
# connect target host by ssh
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname="192.168.223.136",port=22,username="root",password="redhat")
# get the result of target host
result = getlinux(ssh)
# close ssh connection
ssh.close()
print(str(result)+"%s used")
2、获取内存使用率
#!/usr/bin/python
#coding=utf8
#target: get the memory's used of linux system import paramiko def getlinux(ssh):
# executing command
stdin, stdout, stderr = ssh.exec_command(r"free -m|awk 'NR==2{print (($3 - $6 - $7)/$2)*100}'")
err = stderr.readlines()
if len(err) > 0:
return err
else:
stdout_content = stdout.readlines()
result = stdout_content
if len(result) == 0:
print("there is something wrong when executing free -m")
else:
return round(float(result[0].strip()),2) if __name__ == '__main__':
# create ssh object
ssh = paramiko.SSHClient()
# connect target host by ssh
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname="192.168.223.136",port=22,username="root",password="redhat")
result = getlinux(ssh)
ssh.close()
print(str(result)+"% used")
3、获取磁盘使用率:
#!/usr/bin/python
#coding=utf8
#target: get the used of disk import paramiko def getlinux(ssh):
stdin, stdout, stderr = ssh.exec_command(r"df -h|awk 'NR > 1{if($1==$NF){print $1}else{print $0}}'")
err = stderr.readlines()
if len(err) > 0:
return err
else:
stdout_content = stdout.readlines()
result = stdout_content
if len(result) == 0:
print("there is something wrong when executing command")
else:
return result if __name__ == '__main__':
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname="192.168.223.136",port=22,username="root",password="redhat")
result = getlinux(ssh)
ssh.close()
for i in result:
print("the used of %s has used %s" % (i.strip().split()[5],i.strip().split()[4]))
通过paramiko模块在远程主机上执行命令的更多相关文章
- Python3学习之路~9.1 paramiko模块:实现ssh执行命令以及传输文件
我们一般使用linux的时候,都是在Windows上安装一个ssh客户端连接上去.那么从一台linux如何连接到另一条linux呢?使用ssh命令即可,因为每台linux机器自己都有一个ssh客户端. ...
- ansible 批量在远程主机上执行命令
ansible 和 saltstack 都是为了同时在多台主机上执行相同的命令, 但是 salt配置麻烦,ansible基本不用配置, ansible 通过ssh来连接并控制被控节点 1. 安装 第一 ...
- ansible使用shell模块在受控机上执行命令(ansible2.9.5)
一,ansible的shell模块和command模块的区别? shell模块:在远程主机上执行主控端发出的shell/python脚本 command模块:不能调用shell指令,没有bash的环境 ...
- 【Shell实战】批量在多台服务器上执行命令
功能说明:批量在多台服务器上执行命令 #!/bin/bash # ========================================== # 功能:批量在多台服务器上执行命令 # 方法: ...
- expect实现远程主机自动执行命令脚本
2014年第一个脚本,哈哈!!! expect实现远程主机自动执行命令脚本: #!/usr/bin/expect -- if { [llength $argv] < 4 } { puts &qu ...
- 【Python】模块学习之使用paramiko连接Linux,远程执行命令,上传下载、文件
本文主要介绍paramiko远程执行linux命令,及在服务器上进行文件的上传.下载 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. ...
- linux免交互登陆远程主机并执行命令(密钥对和Expect)
原文章摘自:http://lizhenliang.blog.51cto.com/7876557/1607723/ Linux下实现免交互登陆一般有两种: 1. SSH无密码认证方式 客户端使用ssh- ...
- ssh在本地调用远程主机上的命令,不登录远程主机shell
需求描述: 在实际shell脚本的编写过程中,需要通过ssh远程执行一个命令,并返回执行的结果 简单来说,就是将命令发送到远程的主机上进行执行,但是并没有实际的登录到远程主机上.即通过 ssh的方式本 ...
- Ubuntu 上 执行命令 java -version 显示 没有那个文件或目录
解决方法 执行 which java 发现默认java目录:/usr/bin/java . 查看 JAVA_HOME 路径:$JAVA_HOME,得到 /usr/local/java/jdk1.7.0 ...
随机推荐
- iOS 笔试题
转:http://blog.sina.com.cn/s/blog_b0c5954101014upb.html 1.截取字符串”20|http://www.621life.com“ 中 ‘|’字符前面及 ...
- Java使用Apache POI进行Excel导入和导出
Manve依赖 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> ...
- 事件处理程序中 this 的指向
js 高程 事件处理程序 章节原话(斜体表示): 13.2.1 HTML事件处理程序 在这个函数内部,this 值等于事件的目标元素,例如: <!-- 输出 "Click Me&quo ...
- style,currentStyle和getComputedStyle的区别
样式表有三种方式 内嵌样式(inline Style) :是写在Tag里面的,内嵌样式只对所有的Tag有效. 内部样式(internal Style Sheet):是写在HTML的里面的,内部样式只对 ...
- org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade 解决方案 (网络转载)
前提是配置了cascade=all,依然报这种错误,其实出现这个错误的大多数情况根本不是像网上的帖子所说的是什么级联删除的问题,而且hibernate session关于实体生命周期操作的原因,这里明 ...
- jsp+servlet+mvc模式图
在我们的开发中,最常用的开发模式莫过于MVC模式,即M--MODEL.V--View.C--Controller,这样不仅可以方便开发人员分工协作,提高开发效率,增强程序的可维护性和拓展性,而且利用C ...
- c# 日常记录,(获取系统时间、return),一些文件隐藏无法引用,c#多个窗体之间传值
1.获取系统时间 DateTime.Now.ToString(); DateTime dt =DateTime.Now; dt.AddDays(1); //增加一天 dt.AddDays(-1);// ...
- oracle 归档空间满的解决办法
问题现象: 通过命令提示符登陆数据库,一般提示“ora-03113:通信通道的文件结尾”错误,查看trace日志,可以看到详细信息.部分摘录如下(橙色部分给出了建议方案): Errors in fil ...
- PHP error_log 新认知
//error_log 简介及使用方法 // error_log("消息","类型","路径"); //message //type ...
- rest_framework 之分页器
一 分页器--准备 1.1 基本参数 # 普通分页 from rest_framework.pagination import PageNumberPagination # 偏移分页 from r ...