安装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模块在远程主机上执行命令的更多相关文章

  1. Python3学习之路~9.1 paramiko模块:实现ssh执行命令以及传输文件

    我们一般使用linux的时候,都是在Windows上安装一个ssh客户端连接上去.那么从一台linux如何连接到另一条linux呢?使用ssh命令即可,因为每台linux机器自己都有一个ssh客户端. ...

  2. ansible 批量在远程主机上执行命令

    ansible 和 saltstack 都是为了同时在多台主机上执行相同的命令, 但是 salt配置麻烦,ansible基本不用配置, ansible 通过ssh来连接并控制被控节点 1. 安装 第一 ...

  3. ansible使用shell模块在受控机上执行命令(ansible2.9.5)

    一,ansible的shell模块和command模块的区别? shell模块:在远程主机上执行主控端发出的shell/python脚本 command模块:不能调用shell指令,没有bash的环境 ...

  4. 【Shell实战】批量在多台服务器上执行命令

    功能说明:批量在多台服务器上执行命令 #!/bin/bash # ========================================== # 功能:批量在多台服务器上执行命令 # 方法: ...

  5. expect实现远程主机自动执行命令脚本

    2014年第一个脚本,哈哈!!! expect实现远程主机自动执行命令脚本: #!/usr/bin/expect -- if { [llength $argv] < 4 } { puts &qu ...

  6. 【Python】模块学习之使用paramiko连接Linux,远程执行命令,上传下载、文件

    本文主要介绍paramiko远程执行linux命令,及在服务器上进行文件的上传.下载 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. ...

  7. linux免交互登陆远程主机并执行命令(密钥对和Expect)

    原文章摘自:http://lizhenliang.blog.51cto.com/7876557/1607723/ Linux下实现免交互登陆一般有两种: 1. SSH无密码认证方式 客户端使用ssh- ...

  8. ssh在本地调用远程主机上的命令,不登录远程主机shell

    需求描述: 在实际shell脚本的编写过程中,需要通过ssh远程执行一个命令,并返回执行的结果 简单来说,就是将命令发送到远程的主机上进行执行,但是并没有实际的登录到远程主机上.即通过 ssh的方式本 ...

  9. Ubuntu 上 执行命令 java -version 显示 没有那个文件或目录

    解决方法 执行 which java 发现默认java目录:/usr/bin/java . 查看 JAVA_HOME 路径:$JAVA_HOME,得到 /usr/local/java/jdk1.7.0 ...

随机推荐

  1. iOS 界面翻转切换动画

    [UIView  beginAnimations:nil context:NULL]; [UIView setAnimationCurve:UIViewAnimationCurveLinear]; [ ...

  2. anguglar 2+ md5加密

    1.安装 npm install ts-md5 --save 2.使用 import {Md5} from "ts-md5/dist/md5"; constructor() {co ...

  3. cpuspeed和irqbalance服务器的两大性能杀手

    启用 irqbalance 服务,既可以提升性能,又可以降低能耗. irqbalance 用于优化中断分配,它会自动收集系统数据以分析使用模式,并依据系统负载状况将工作状态置于 Performance ...

  4. 并发编程 - IO模型 - 1.io模型/2.阻塞io/3.非阻塞io/4.多路复用io

    1.io模型提交任务得方式: 同步:提交完任务,等结果,执行下一个任务 异步:提交完,接着执行,异步 + 回调 异步不等结果,提交完任务,任务执行完后,会自动触发回调函数同步不等于阻塞: 阻塞:遇到i ...

  5. 汉诺塔IV---hdu2077

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2077 #include <stdio.h> #include <stdlib.h&g ...

  6. mac配置python自然语言处理环境

    一.nltk安装 Ⅰ.工具安装步骤 1.根据python版本从 https://pypi.python.org/pypi/setuptools 下载对应版本的setuptools.然后,在终端下运行, ...

  7. 七牛云存储--内存put示例(go sdk)

    啥都不说了,居然有文档,有git为啥不提供example? 自己看代码,琢磨了一下,原来是要这么用的.这里不得不吐槽一下package的命名,为啥要去io?golang自带系统包名就有io啊,哥哥. ...

  8. 前端之masonry(图片瀑布流插件)

    加载代码: 1 2 <script src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script ...

  9. docker部署oracle

    oracle部署 创建oracle用户 [root@stpass-15 ~]# useradd oracle[root@stpass-15 oracle]# cd oracle [root@stpas ...

  10. linux 实用命令

    从一台机器远程连接到另一台机器: ssh platform@192.168.155.116 从一台机器发送文件到另一台机器: scp /home/weihuang/vie-zyzj.jar platf ...