• paramiko模块学习


    import paramiko
    import psutil
    
    
    def bytes2human(n):
        """
        字节转化成相应容量值
        :param n: 字节数
        :return:
        """
        # 容量单位
        symbols = ('K','M','G','T')
        prefix = {}
        for i, s in enumerate(symbols):
            prefix[s] = 1 << ( i + 1 ) * 10
        # prefix = {'K': 1024, 'M': 1048576, 'G': 1073741824, 'T': 1099511627776}
        for s in reversed(symbols):
            if n >= prefix[s]:
                value = float(n) / prefix[s]
                return '%.1f%s' % (value,s)
        return '%sB' % n
    
    class Conn:
        def __init__(self, ip, username, password, port=22):
            self.ip = ip
            self.port = port
            self.username = username
            self.password = password
    
        def ssh_conn(self):
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
            ssh.connect(self.ip, self.port, self.username, self.password)
            return ssh
    
    
    
    
    def check_file(ip, port, username, password):
        """
        远程执行命令查看服务器的文件系统使用情况
        在linux系统下可以免密,利用秘钥登录
        :param ip: 目标机器IP
        :param port: 端口号
        :param username: 用户名
        :param password: 密码
        :return:
        """
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
        ssh.connect(ip, port, username, password)
        _, out, _ = ssh.exec_command('df -h;df -i')
        ret = out.readlines()
        # print(ret)
        for line in ret:
            if '文件系统' not in line:
                line = line.strip().split()
                file_num = int(line[4].replace('%', ' '))
                file_system = line[5]
                # print(file_num,file_system)
                if file_num > 60:
                    print(file_num, file_system)
    
        ssh.close()
    
    
    def check_file1(ip, username, password):
        conn1 = Conn(ip, username, password)
        ssh = conn1.ssh_conn()
        _, out, _ = ssh.exec_command('df -h;df -i')
        ret = out.readlines()
        for line in ret:
            if '文件系统' not in line:
                line = line.strip().split()
                file_num = int(line[4].replace('%', ' '))
                file_system = line[5]
                if file_num > 60:
                    print(file_num, file_system)
        ssh.close()
    
    
    def check_mem(ip,username,password):
        conn = Conn(ip=ip,username=username,password=password)
        ssh = conn.ssh_conn()
    
    
    if __name__ == '__main__':
        
        ret = bytes2human(1024)
        print(ret)
    def check_file(ip,port,username,password):
        """
        远程执行命令查看服务器的文件系统使用情况
        在linux系统下可以免密,利用秘钥登录
        :param ip: 目标机器IP
        :param port: 端口号
        :param username: 用户名
        :param password: 密码
        :return:
        """
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
        ssh.connect(ip,port,username,password)
        _, out, _ = ssh.exec_command('df -h;df -i')
        ret = out.readlines()
        # print(ret)
        for line in ret:
            if '文件系统' not in line:
                line = line.strip().split()
                file_num = int(line[4].replace('%', ' '))
                file_system = line[5]
                # print(file_num,file_system)
                if file_num > 60:
                    print(file_num, file_system)
    ssh.close()

      

  • 相关阅读:
    陈应松《母亲》
    黄灯:一个农村儿媳眼中的乡村图景
    喝完茶为什么嘴里是甜的
    俗语一千条
    XtraBackup完整备份与增量备份的原理
    李嘉诚:90%考虑失败 关注细节
    redo和undo的区别
    tar命令
    自增锁引发的悲剧
    各版本 MySQL 并行复制的实现及优缺点
  • 原文地址:https://www.cnblogs.com/superniao/p/10585817.html
Copyright © 2020-2023  润新知