Python-SSH连接工具类

使用Paramiko模块进行SSH连接的工具类

import paramiko
from time import sleep

class SSH_Class:
    hostIP = '192.168.0.2'
    TCP_Port = 22
    userName = 'root'
    passWord = 'root'
    keyFilePath = ''
    keyFilePassword = ''
    timeOut = 60
    """初始化SSH_Class,参数赋初值可缺省"""
    def __init__(self, hostIP='', TCP_Port=22, userName='', passWord='', keyFilePath='', keyFilePassword='', timeOut=60, commands=[]):
        self.hostIP = hostIP
        self.TCP_Port = TCP_Port
        self.userName = userName
        self.passWord = passWord
        self.keyFilePath = keyFilePath
        self.keyFilePassword = keyFilePassword
        self.timeOut = timeOut
        self.commands = commands
    def getSshObject(self):
        """创建SSH对象,用于SSH连接使用,并返回SSH paramiko对象"""
        ssh_Object = paramiko.SSHClient()
        ssh_Object.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        if ssh_Object:
            return ssh_Object
        else:
            self.closeSshObject(ssh_Object)
    def connectByKey(self):
        """通过密钥进行SSH连接方法,返回SSH的连接对象"""
        if self.keyFilePassword:
            RSA_Key = paramiko.RSAKey.from_private_key_file(self.keyFilePath,password=self.keyFilePassword)
        else :
            RSA_Key = paramiko.RSAKey.from_private_key_file(self.keyFilePath)
        ssh_Object = self.getSshObject()
        ssh_Object.connect(hostname=self.hostIP,port=self.TCP_Port,username=self.userName,pkey=RSA_Key,timeout=self.timeOut)
        if ssh_Object:
            return ssh_Object
        else:
            self.closeSshObject(ssh_Object)
            raise Exception("The SSH Key Connection Failed!")
    def connectByPassword(self):
        """通过密码进行SSH连接方法,返回SSH的连接对象"""
        ssh_Object = self.getSshObject()
        ssh_Object.connect(hostname=self.hostIP,port=self.TCP_Port,username=self.userName,password=self.passWord,timeout=self.timeOut)
        if ssh_Object:
            return ssh_Object
        else :
            self.closeSshObject(ssh_Object)
            raise Exception("The SSH Password Connection Failed!")
    def closeSshObject(self, ssh_Object):
        """关闭SSH连接对象"""
        if ssh_Object:
            ssh_Object.close()
        else:
            raise Exception("Failed To Close SSH Connection!")
    def executeSshCommands(self, ssh_Object, My_Commands):
        """连接完成后执行命令,使用时具体细节请参照实际连接过程及命令执行过程"""
        result = ''
        if My_Commands:
            command = ssh_Object.invoke_shell()
            for com in My_Commands:
                sleep(1)
                command.send(com)
                out = command.recv(1024)
                #print(out.decode())
                result=result+(out.decode())
            return result
        else :
            self.closeSshObject(ssh_Object)
            raise Exception("No Commands are Executed!") 



if __name__ == '__main__':
    """连接示例"""
    My_Commands=['\n','\n','ls','\n','\n']
    #使用hostIP='192.168.0.2'传参可进行缺省参数传参
    sshClass = SSH_Class(hostIP='192.168.0.2', userName='root', passWord='root')
    sshObject = sshClass.connectByPassword()
    res = sshClass.executeSshCommands(sshObject,My_Commands)
    sshClass.closeSshObject(sshObject)
    print(res)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值