python 通过ssh私钥连接服务器下载文件

Jackey Python 1,600 次浏览 , 没有评论
#!/usr/bin/python3

import os

import paramiko


class DownFile(object):
    ssh = paramiko.SSHClient()

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.ssh.close()

    # 链接ssh
    def sshLogin(self):
        privateKey = paramiko.RSAKey.from_private_key_file('/Users/jackey/Downloads/ssh/id_rsa')
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh.connect(hostname='127.0.0.1', username='root', port=22, pkey=privateKey)
        return

    # 下载文件到本地
    def downFile(self, date, localFileName):
        remoteFileName = '/data/clean_data/tick.r.taoso.com/tick.log-' + date + '.gz'
        sftp = self.ssh.open_sftp()
        sftp.get(remoteFileName, localFileName)
        sftp.close()
        return

    # 解压缩本地文件
    @staticmethod
    def gzipFile(localFileName):
        os.system('gzip -d ' + localFileName)
        return


if __name__ == '__main__':
    dateTime = '20230110'
    localFIleNme = 'tick.log-' + dateTime + '.gz'
    f = DownFile()
    f.sshLogin()
    f.downFile(dateTime, localFIleNme)
    f.gzipFile(localFIleNme)

 

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Go