[Python]paramiko实时获取命令执行输出及sftp上传

import paramiko, os

class preExecute():
    def __init__(self, _hostip, _username, _password):
        self.__channel = group
        self.__hostip = _hostip

        self.__username = _username
        self.__password = _password
        self.__sshclient = self.ssh_connect(self.__hostip, self.__username, self.__password)
        self.__currentpath = os.getcwd()
        self.__destdir = "/opt/uniagentdfr/"
        self.__sftpclient = None
    def __del__(self):
        self.__sshclient.close()
        try:
            self.__sftpclient.close()
        except:
            pass

    def run(self):
        self.execute('mkdir -p '+self.__destdir)
        filename = self.__currentpath+"/controller/static/controller/module.zip"
        self.sftp_put(filename, self.__destdir + "module.zip")
        
        self.execute("cd "+self.__destdir+ ";")
        self.execute("touch /opt/uniagentdfr/aaaaa;")
        self.execute("unzip -o /opt/uniagentdfr/module.zip  -d /opt/uniagentdfr/;")
        self.execute("chmod -R 755 *;")
       
        self.__del__();

    def ssh_connect(self,_host, _username, _password):
        try:
            _ssh_fd = paramiko.SSHClient()
            _ssh_fd.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            _ssh_fd.connect(_host, username=_username, password=_password)
            return _ssh_fd
        except Exception as e:
            print('ssh %s@%s:passwd %s, error message is %s' % (_username, _host,_password, e))



    def sftp_open(self):
        if self.__sftpclient is None:
            self.__sftpclient = self.__sshclient.open_sftp()
        return ;

    def sftp_put(self, _put_from_path, _put_to_path):
        self.sftp_open()
        return self.__sftpclient.put(_put_from_path, _put_to_path)

    def sftp_get(self,_get_from_path, _get_to_path):
        self.__sftpclient = self.sftp_open(self.__sshclient)
        self.__sftpclient.get(_get_from_path, _get_to_path)
        self.__sftpclient.close()
        self.__sftpclient = None
        return ;

    def execute(self, command):
        try:
            stdin, stdout, stderr = self.__sshclient.exec_command(command)
            for line in stdout:
                print(line.strip("\n"))
        except Exception as e:
            print("execute command %s error, error message is %s" % (command, e))
            return ""

你可能感兴趣的:(python)