通过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 ...
随机推荐
- LAMP集群项目五 nfs存储的数据实时同步到backupserver
tar fxzsersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/ mv GNU-Linux-x86 sersync cp sers ...
- 1:TwoSum(如果两个和为某个数,找出这俩数的位置)
package leetcode; import java.util.HashMap; import java.util.Map; /** * @author mercy *Example: *Giv ...
- ERROR in [copy-webpack-plugin] unable to locate "D:/xxx/xxx/xxx"
vue打包的时候npm run build报错 ERROR in [copy-webpack-plugin] unable to locate "D:/xxx/xxx/xxx" 这 ...
- FreeMarker 的使用方法
1.FreeMarker 概述 FreeMarker 是一个用Java语言编写的模板引擎,使用模板来生成文本输出;主要用于做静态页面或页面展示; 2.FreeMarker 使用 // 导入jar包: ...
- 剑指Offer——链表中环的入口结点
题目描述: 一个链表中包含环,请找出该链表的环的入口结点. 分析: 设置两个指针p1,p2, 两个指针都从链表的头部开始走,不过p1每次走一步,p2每次走两步. 直到相遇的时候,p2走的长度是p1的两 ...
- C++应该被看成是个语言集合——四种语言(C语言,OO语言,泛型语言,STL)
至少有三种语言: 一,C++ is C 二,C++ is an OO language 三,C++ is a genetic programming language 有的童鞋觉得难,可能是没有看清楚 ...
- 多进程使用matplotlib.pyplot绘heatmap(多线程不可以)
数据格式如下: 8_15/l_eye/2732.png -20.5773 -5.17769 -3.34583 21.5859 9_13_1/l_eye/1211.png -10.1145 34.992 ...
- 前端构建工具gulpjs的使用介绍及技巧(一)
原文链接:http://www.cnblogs.com/2050/p/4198792.html gulpjs是一个前端构建工具,与gruntjs相比,gulpjs无需写一大堆繁杂的配置参数,API也非 ...
- git 的 pull、fetch、merge
1.pull = fetch + merge In the simplest terms, git pull does a git fetch followed by a git merge. You ...
- oracle入门(6)——PL/SQL常用语法
[本文介绍] 本文不是”语法大全“,只是记录下作项目里自己常用的一些语法.方便查询. [语法] [输出] (1)输出语法 DBMS_OUTPUT.PUT_LINE( ) [定义] (1)定义变 ...