Getting file matching a pattern from the remote server which matches current date using python
There is a directory in the remote server '/root/' and it contains multiple files. I want to get a file of today's date whose format is 'SleepingCell_*' in my local directory. If this file is not present on today's date then return nothing. Please note: today's date is the date on the remote server (different from local server)
Example: remote_path = '/root/' host = '10.4.5.6' user = 'AshuT' password = 'AshuTest123' client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(host, username=user, password=password) sftp = client.open_sftp() #sftp_file = [] for i in sftp.listdir(remote_path): lstatout=str(sftp.lstat(i)).split()[0] if 'd' not in lstatout: if fnmatch.fnmatch(i, "SleepingCell_*"): #code to check if the file's create/update time is today's date |
Regular users like AshuT don't have access (not even the read-only one) to the /root directory.
Ubuntu 20.04: Code:
$ ls -ld /root Code:
$ ls -ld /root Aside from that, please use code tags when posting code. Especially Python. |
Quote:
|
Quote:
Consider using rsync. Rsync, when given the filename to transfer, will either transfer it or not, depending on if it is there. error messages can be ignored if desired. Rsync uses ssh so it is secure. It seems you might be expecting to do this repeatedly so consider a cron job to do so. It would be very easy to use the date command to find the date/time on the client, add an adjustment for the date/time difference on the server, then send the full file name to be retrieved. |
All times are GMT -5. The time now is 12:19 AM. |