LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Getting file matching a pattern from the remote server which matches current date using python (https://www.linuxquestions.org/questions/linux-newbie-8/getting-file-matching-a-pattern-from-the-remote-server-which-matches-current-date-using-python-4175688994/)

AshuTrip 01-21-2021 04:35 AM

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

shruggy 01-21-2021 05:11 AM

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
drwx------ 10 root root 4096 Jul 21  2020 /root

CentOS 7:
Code:

$ ls -ld /root
dr-xr-x---. 8 root root 4096 26. Jan 2020  /root

It is also a bad idea to keep some random files there and access them remotely. Better think of another place on the remote server where you could keep those SleepingCell_* files.

Aside from that, please use code tags when posting code. Especially Python.

AshuTrip 01-21-2021 05:41 AM

Quote:

Originally Posted by shruggy (Post 6210248)
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
drwx------ 10 root root 4096 Jul 21  2020 /root

CentOS 7:
Code:

$ ls -ld /root
dr-xr-x---. 8 root root 4096 26. Jan 2020  /root

It is also a bad idea to keep some random files there and access them remotely. Better think of another place on the remote server where you could keep those SleepingCell_* files.

Aside from that, please use code tags when posting code. Especially Python.

the /root directory is just for example.. it could be anything

computersavvy 01-21-2021 11:42 AM

Quote:

Originally Posted by AshuTrip (Post 6210260)
the /root directory is just for example.. it could be anything

First look at permissions.
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.