如何获取包含 Linux 中感兴趣的特定文件的最新文件夹并在 Python 中使用 Paramiko 下载该文件? [英] How to get the latest folder that contains a specific file of interest in Linux and download that file using Paramiko in Python?

查看:61
本文介绍了如何获取包含 Linux 中感兴趣的特定文件的最新文件夹并在 Python 中使用 Paramiko 下载该文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 3 中的 Paramiko 将特定文件从远程服务器 scp 到我的本地机器.

I am trying to scp a specific file from a remote server to my local machine using Paramiko in Python 3.

背景:目标机器 198.18.2.2 上有一个目录 mydir,其中包含许多以名称 2020...

Background: There is a directory mydir on the destination machine 198.18.2.2 that contains many timestamp directories that start with the name 2020...

目标机器:198.18.2.2

源机器:198.18.1.1

到目前为止,我已经设法构造了要执行的命令,如下所示 -

So far I have managed to construct the command to be executed as follows -

cd "$(ls -1d /mydir/20* | tail -1)"; scp -o StrictHostKeyChecking=no email_summary.log root@198.18.1.1:/mydir/work/logs/email_summary_198.18.2.2.log

代码:

def remote_execute(dest_ip, cmd):
    """API to execute command on remote machine"""
    result = []
    sys.stderr = open('/dev/null')
    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        ssh_client.connect(dest_ip, username='root')
        stdin, stdout, stderr = ssh_client.exec_command(cmd)
        for line in stdout.readlines():
            result.append(line.strip())
        ssh_client.close()
        return result
    except paramiko.AuthenticationException:
        print("Authentication with the remote machine failed")
        return
    except paramiko.SSHException:
        print("Connection to remote machine failed")
        return
    except paramiko.BadHostKeyException:
        print("Bad host key exception for remote machine")
        return

调用:remote_execute('198.18.1.1', cmd)

问题是ls -1d/mydir/20* |tail -1 总是给我最新的时间戳文件夹.但是,如果该文件夹中不存在 email_summary.log 文件,我想查看下一个包含文件 email_summary.log 的最新时间戳文件夹.

The problem is ls -1d /mydir/20* | tail -1 always gives me the latest timestamp folder. But if the email_summary.log file is not present in that folder, I would like to look into next latest timestamp folder that has the file email_summary.log.

本质上,从包含文件email_summary.log"的最新时间戳文件夹中 scp 文件.有人可以帮我解决这个问题吗?

Essentially, scp the file from the latest timestamp folder that contains the file "email_summary.log". Can someone please help me with this?

提前致谢.

推荐答案

在远程机器上执行 scp 命令以将文件push 回本地机器是一种矫枉过正.而且一般来说,依赖 shell 命令是非常脆弱的方法.您最好仅使用本机 Python 代码,以识别最新的远程文件并将其到您的本地计算机.您的代码将更加健壮和可读.

Executing scp command on the remote machine to push the file back to the local machine is an overkill. And in general relying on shell commands is very fragile approach. You better use native Python code only, to identify the latest remote file and pull it to your local machine. Your code will be way more robust and readable.

sftp = ssh.open_sftp()
sftp.chdir('/mydir')

files = sftp.listdir_attr()

dirs = [f for f in files if S_ISDIR(f.st_mode)]
dirs.sort(key = lambda d: d.st_mtime, reverse = True)

filename = 'email_summary.log'

for d in dirs:
    print('Checking ' + d.filename)
    try:
        path = d.filename + '/' + filename
        sftp.stat(path)
        print('File exists, downloading...')
        sftp.get(path, filename)
        break
    except IOError:
        print('File does not exist, will try the next folder')

以上内容基于:

旁注:不要使用 AutoAddPolicy.这样做你会失去安全感.请参阅Paramiko未知服务器".

Side note: Do not use AutoAddPolicy. You lose security by doing so. See Paramiko "Unknown Server".

这篇关于如何获取包含 Linux 中感兴趣的特定文件的最新文件夹并在 Python 中使用 Paramiko 下载该文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆