LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-21-2021, 04:35 AM   #1
AshuTrip
LQ Newbie
 
Registered: Aug 2018
Posts: 4

Rep: Reputation: Disabled
Post 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
 
Old 01-21-2021, 05:11 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
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.

Last edited by shruggy; 01-21-2021 at 05:23 AM.
 
Old 01-21-2021, 05:41 AM   #3
AshuTrip
LQ Newbie
 
Registered: Aug 2018
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
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
 
Old 01-21-2021, 11:42 AM   #4
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,342

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Quote:
Originally Posted by AshuTrip View Post
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.
 
  


Reply

Tags
python3


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
vi search multiple pattern and remove only exact matching pattern amateurscripter Linux - Newbie 4 05-07-2018 01:19 PM
Matching patterns or partial pattern matching yaplej Programming 6 12-16-2012 10:21 AM
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 12:17 PM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
Bash File Name Matching - Binary file .ogg matches !!! maxvonseibold Linux - General 8 01-30-2007 06:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration