Saved has asked for the wisdom of the Perl Monks concerning the following question:

I would like to get the TimeStamp of a file on a remote system to deside if I need to pull it. What I tried in perl script

@Time= qx/ssh $Uat "\/usr\/bin\/perl -e '$MTIME = ( stat ( Envs_${Uat +}.xml ))[9];print $MTIME;'/";

I also tried from the bash command line...

 ssh ifcaa1 BOB=`perl -e '$TIME=time(); print $TIME;'`;echo $BOB

Just to get something simple working... not root & "Net::SSH" not an option. All Help much appreciated

Replies are listed 'Best First'.
Re: Get remote file TimeStamp
by Eliya (Vicar) on Feb 28, 2012 at 18:11 UTC

    You don't necessarily need Perl for that.

    stat -c%Y somefile

    should also output the modification time (in seconds since the epoch — if you want human readable form, use format %y instead).  Simply run the command via ssh (which I presume you are allowed to do).

      Excellent, works fine, I'll go with that. Thanx.

      This was fine on Linux/bash , but not HP-UX/ksh. I then got this working at the command line

      ssh -q aimuat perl -e \'\$MTIME = \( stat \( \"Envs_aimuat.xml\" \)\)\[9\]\;print \$MTIME\;\'

      But when I put in into a perl script it fails

      $TStamp=`ssh -v aimuat perl -e '\$MTIME = \( stat \( \"Envs_aimuat.xml +\" \)\)\[9\]\;print \$MTIME\;'`; print "$TStamp";
      Any Ideas? Thanx

        The backticks "eat" one level of escapes, when they occur literally within the backticks.  So, you either need to double escape them, or maybe better (to work around the ugliness), put the command in a variable, which you then interpolate into the backticks:

        my $cmd = q|ssh -q aimuat perl -e \'\$MTIME = \( stat \( \"Envs_aimuat +.xml\" \)\)\[9\]\;print \$MTIME\;\'|; $TStamp = `$cmd`;

        P.S., you can do away with the $MTIME variable if you use a "+" to syntactically disambiguate the argument to the print function:

        my $cmd = q|ssh -q aimuat perl -e \'print +\( stat \( \"Envs_aimuat.xm +l\" \)\)\[9\]\;\'|; $TStamp = `$cmd`;
Re: Get remote file TimeStamp
by JavaFan (Canon) on Feb 28, 2012 at 19:11 UTC
    I would like to get the TimeStamp of a file on a remote system to deside if I need to pull it.
    Or let rsync do the syncing, and let it worry about getting the timestamps?
Re: Get remote file TimeStamp
by runrig (Abbot) on Feb 29, 2012 at 19:56 UTC
    With Net::SFTP::Foreign, the "ls" and "find" methods return, among other things, the modification time of files (and so does "stat" as salva shows above).
Re: Get remote file TimeStamp
by salva (Canon) on Feb 29, 2012 at 19:56 UTC
    use Net::SFTP::Foreign; my $sftp = Net::SFTP::Foreign->new($Uat); my $attr = $sftp->stat("Envs_${Uat}.xml") or die "unable to stat remot +e file"; printf "modification time: %d\n", $attr->mtime;