zoukankan      html  css  js  c++  java
  • 通过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]))
    

      

  • 相关阅读:
    让框架内循环或者指定元素 指定CSS
    织梦dedecms自定义功能函数(1):调用body中的图片(可多张)
    dedecms 上传目录路径
    点击展示菜单11
    ueditor 去掉自动跟随内容的<p><br /></p>
    点击切换JS
    点击切换的JS
    Unity3D NGUI 给button按钮添加单间事件
    企业如何留住核心人才[转载分享]
    java SSH框架详解(面试和学习都是最好的收藏资料)
  • 原文地址:https://www.cnblogs.com/jsonhc/p/7307476.html
Copyright © 2011-2022 走看看