python ssh

这个Python类实现了SSH连接的功能,包括通过密钥和密码方式连接远程服务器,执行命令并获取返回结果,以及关闭SSH连接。类中包含了`conn_by_key`和`conn_by_pwd`方法分别用于密钥和密码认证,`exec_command`方法用于执行命令并打印输出,最后`close`方法用于关闭连接。
摘要由CSDN通过智能技术生成
import paramiko


class SshEngine:
    """
    ssh连接对象
    本对象提供了密钥连接、密码连接、命令执行、关闭连接
    """
    ip = ''
    port = 22
    username = ''
    timeout = 0
    ssh = None

    def __init__(self, ip, username, port=22, timeout=30):
        """
        初始化ssh对象
        :param ip: str  主机IP
        :param username: str  登录用户名
        :param port: int  ssh端口
        :param timeout: int  连接超时
        """
        self.ip = ip
        self.username = username
        self.port = port
        self.timeout = timeout
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh = ssh

    def conn_by_key(self, key):
        """
        密钥连接
        :param key: str  rsa密钥路径
        :return: ssh连接对象
        """
        rsa_key = paramiko.RSAKey.from_private_key_file(key)
        self.ssh.connect(hostname=self.ip, port=self.port, username=self.username, pkey=rsa_key, timeout=self.timeout)
        if self.ssh:
            return self.ssh
        else:
            self.close()
            raise Exception("密钥连接失败.")

    def conn_by_pwd(self, pwd):
        """
        密码连接
        :param pwd: str  登录密码
        :return: ssh连接对象
        """
        self.ssh.connect(hostname=self.ip, port=self.port, username=self.username, password=pwd)
        if self.ssh:
            return self.ssh
        else:
            self.close()
            raise Exception("密码连接失败.")

    def exec_command(self, command):
        """
        命令控制
        :param command: str  命令
        :return: dict  命令执行的返回结果
        """
        if command:
            #stdin, stdout, stderr = self.ssh.exec_command(command)
            #return stdout
            shell = self.ssh.invoke_shell()
            shell.settimeout(1)
            shell.send(command)
            print("----the command is ",command)
            recv = shell.recv(20480).decode(encoding='UTF-8')
            #recv = shell.recv(512).decode()
            #recv=shell.recv()
            # str_data = ""
            # while True:
            #     try:
            #         recv = shell.recv(20480).decode(encoding='UTF-8')
            #         if recv:
            #             print("11111111111")
            #
            #             str_data.append(recv)
            #             #if "=" in recv:
            #              #   print("  == in recv ",recv)
            #               #  return recv
            #
            #             print(str_data)
            #
            #         else:
            #             continue
            #     except:
            #         print("the except is recv:",str_data)
            #         return recv
                    #command = input(">>>") + "\n"
                    #shell.send(command)
            print("-----------------redv----------")

            return recv
        else:
            self.close()
            raise Exception("命令不能为空字符串.")

    def close(self):
        """
        关闭当前连接
        :return:
        """
        if self.ssh:
            self.ssh.close()
        else:
            raise Exception("ssh关闭失败,当前对象并没有ssh连接.")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘多拉的面

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值