LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 11-09-2014, 09:26 PM   #1
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Rep: Reputation: Disabled
checking file age on a remote machine


hi experts!

im trying to check file age on a remote linux machine using python os.stat. is that possible??

this is my way of checking file age on a local machine :
Code:
for file in glob.glob(location) :
    #print file
    st = os.stat(file)
    mtime = (time.time() - st.st_mtime) / 60
    print mtime
how do i do the same thing on a remote machine?
 
Old 11-10-2014, 10:23 AM   #2
dijetlo
Senior Member
 
Registered: Jan 2009
Location: RHELtopia....
Distribution: Solaris 11.2/Slackware/RHEL/
Posts: 1,491
Blog Entries: 2

Rep: Reputation: Disabled
Well Sim, the easiest way is to ssh to the target host, transfer the script, su and run it.
You can normally manage that with a simple 'expect' script.

Or we have some complex 'Dawn of UNIX' quirky voodoo behind door number 2.
What do you prefer?

Last edited by dijetlo; 11-10-2014 at 10:24 AM.
 
Old 11-10-2014, 09:40 PM   #3
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Original Poster
Rep: Reputation: Disabled
hi there. i got this resolved using paramiko. this is my code.
below is my code to achieve this

Code:
nbytes = 4096
hostname = '10.86.8.129'
port = 22
username = 'root'
password = '123456'
command = 'stat -c%Y /usr/local/testlogs/logfiles/*'

client = paramiko.Transport((hostname, port))
client.connect(username=username, password=password)

stdout_data = []
stderr_data = []
session = client.open_channel(kind='session')
session.exec_command(command)


while True:
    if session.recv_ready():
        stdout_data.append(session.recv(nbytes))
    if session.recv_stderr_ready():
        stderr_data.append(session.recv_stderr(nbytes))
    if session.exit_status_ready():
        break

                #print 'exit status: ', session.recv_exit_status()
                #print ''.join(stdout_data)
                #print ''.join(stderr_data)

    results = ''.join(stdout_data)
    results2 = ''.join(stderr_data)

    result = results.split('\n')
    for val in result :
        if val != val[:-1] :
            age = (time.time() - float(val)) / 60
            if age > 1440 :
                print "File is older than 1 day"
            else :
                print "File is still below 1 day old"
thank you very much for you help!

Last edited by simhumcon; 11-10-2014 at 09:44 PM.
 
Old 11-11-2014, 04:40 AM   #4
dijetlo
Senior Member
 
Registered: Jan 2009
Location: RHELtopia....
Distribution: Solaris 11.2/Slackware/RHEL/
Posts: 1,491
Blog Entries: 2

Rep: Reputation: Disabled
NP.
Since you resolved it, why don't mark it as solved in case somebody else is looking for an answer to the same question
 
Old 11-11-2014, 07:59 PM   #5
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Original Poster
Rep: Reputation: Disabled
sorry about that! i will do that now :-)
 
1 members found this post helpful.
  


Reply



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
[SOLVED] Edit file line on remote machine muhamed.ahmovic Linux - Newbie 7 09-29-2012 02:28 PM
open remote file on local machine gabriel.560 Linux - Software 2 07-31-2012 01:14 AM
file transfer from remote window machine perky.nishi Linux - Newbie 1 06-24-2008 06:29 AM
scp: copy a file from local machine to remote machine seran Linux - Newbie 8 10-30-2007 12:23 PM
file age checking Oinopion Slackware 8 03-11-2006 06:37 AM

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

All times are GMT -5. The time now is 07:38 PM.

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