简体   繁体   English

如何在 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?

I am trying to scp a specific file from a remote server to my local machine using Paramiko in Python 3.我正在尝试在 Python 3 中使用 Paramiko 将特定文件从远程服务器 scp 到我的本地计算机。

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上有一个目录mydir ,里面有很多以2020...

Destination machine: 198.18.2.2目标机器: 198.18.2.2

Source Machine: 198.18.1.1源机器: 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

Code:代码:

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

Call: remote_execute('198.18.1.1', cmd)调用: remote_execute('198.18.1.1', cmd)

The problem is ls -1d /mydir/20* | tail -1问题是ls -1d /mydir/20* | tail -1 ls -1d /mydir/20* | tail -1 always gives me the latest timestamp folder. ls -1d /mydir/20* | tail -1总是给我最新的时间戳文件夹。 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文件,我想查看下一个包含文件email_summary.log的最新时间戳文件夹。

Essentially, scp the file from the latest timestamp folder that contains the file "email_summary.log".本质上,scp 来自包含文件“email_summary.log”的最新时间戳文件夹中的文件。 Can someone please help me with this?有人可以帮我吗?

Thanks in advance.提前致谢。

Executing scp command on the remote machine to push the file back to the local machine is an overkill.在远程机器上执行scp命令将文件送回本地机器是一种矫枉过正的做法。 And in general relying on shell commands is very fragile approach.并且通常依赖 shell 命令是非常脆弱的方法。 You better use native Python code only, to identify the latest remote file and pull it to your local machine.您最好只使用本机 Python 代码,以识别最新的远程文件并将其拉到本地机器。 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')

The above is based on:以上是基于:


Side note: Do not use AutoAddPolicy .附注:不要使用AutoAddPolicy You lose security by doing so.这样做会失去安全感。 See Paramiko "Unknown Server" .请参阅Paramiko“未知服务器”

How about finding for file (not directory) using find ?使用find文件(而不是目录)怎么样?

find /mydir/20* -name email_summary.log | sort | tail -1

This will give you you a path to the latest file to copy.这将为您提供要复制的最新文件的路径。

So, your command will look like that:因此,您的命令将如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python获取最新创建的文件名而不是文件夹名? - How to get the latest created file name and not the folder name using python? 如何使用Paramiko仅从SFTP服务器下载最新文件? - How to download only the latest file from SFTP server with Paramiko? 如何获取文件夹中包含特定单词的文件? - how to get a file that contains a specific word in a folder? 使用python将文件下载到特定文件夹 - Download a file to a specific folder with python 使用Python将最新版本的文件从网站下载到特定位置 - Download latest version of file from website to specific location using Python 如何使用 python 中的 boto3 从 S3 文件夹下载最新文件? - How to download latest file from S3 folder using boto3 in python? 使用 python 获取目录中包含最新时间戳的文件名 - Get the file name which contains the latest timestamp in a directory using python 如何获取文件夹中的最新文件? - How to get the latest file in a folder? 如何获取我的Python脚本以转到URL,下载最新文件 - How to get my Python script to go to a URL, download the latest file 通过在Python中使用Paramiko模块,如何在Linux服务器上编辑文件? - By using Paramiko module in Python how can I edit a file on Linux server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM