当前位置:首页 > Python教程 > python技巧

Python paramiko模块使用解析 封装方法(实现ssh)

            #
             coding=utf-8
            import
             sys, logging



            from paramiko.client import SSHClient, AutoAddPolicy
from paramiko import AuthenticationException
from paramiko.ssh_exception import NoValidConnectionsError
class SshClient:
    def__init__(self, host_ip, username, password):
     # 创建ssh对象
        self.ssh_client = SSHClient()
        self.host_ip = host_ip
        self.username = username
        self.password = password
        self.port = 22

    def__enter__(self):
        try:
            # 设置允许连接known_hosts文件中的主机(默认连接不在known_hosts文件中的主机会拒绝连接抛出SSHException)            self.ssh_client.set_missing_host_key_policy(AutoAddPolicy)
       # 连接服务器
            self.ssh_client.connect(self.host_ip, self.port, self.username, self.password, timeout=60)
        except AuthenticationException as e:
            logging.warning(username or password error)
            raise e
            # return 1001except NoValidConnectionsError as e:
            logging.warning(connect time out)
            raise e
            # return 1002except Exception as a:
            print(Unexpected error:, sys.exc_info()[0])
            raise a
            # return 1003return self

    def excute_command(self, commands):
        # 执行命令# stdin:标准输入(就是你输入的命令);stdout:标准输出(就是命令执行结果);stderr:标准错误# (命令执行过程中如果出错了就把错误打到这里),stdout和stderr仅会输出一个
        stdin, stdout, stderr = self.ssh_client.exec_command(commands)
     # 返回命令结果return stdout.read().decode()

    def__exit__(self, exc_type, exc_val, exc_tb):
     # 关闭服务器连接        self.ssh_client.close()


if__name__ == __main__:
    with SshClient(192.168.2.63, root, password) as ssh_c:
        blk = vdb
        command = "df -h | grep /dev/%s" % blk
        result = ssh_c.excute_command(command)
        if len(result) == 0:
            mount_command = "mount /dev/%s /mnt" % blk
            mount_result = ssh_c.excute_command(mount_command)
            print(mount_result)

 

原文:https://www.cnblogs.com/xuezhimin-esage-2020/p/14470452.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐