2

I'm new to perl. I have a input file with pipe delimited lines which have server_name, and file names as fields. The file names have wild card (*) in the input feed file. The actual file names will be suffixed with timestamps.

Ex: Actual files in remote servers
server_1/abc_20110801.txt
server_1/abc_20110802.txt
server_1/abc_20110803.txt
server_2/xyz_20110801.dat
server_2/xyz_20110802.dat



Ex: Input feed file
server_1|abc_*.txt
server_2|xyz_*.dat

I'm trying to ssh to the remote servers and read the file and capture the timestamps of the 'latest' file (in this case server_1/abc_20110803.txt and server_2/xyz_20110802.dat). ssh keys to all the remote servers are already setup so that we don't need to pass user id and passwords.

I'm able to do this if the files exists in the same server using the following:

my @list_files = map { chomp; $_ } (`ls -t $wild_f_nme`);
my $f_nme=$list_files[0];
my ($accesstime, $modtime, $createtime, $fsize) = (stat($f_nme))[8,9,10,7];

How would I do this to check the time stamps of files in remote servers. I need to initiate ssh connection for each of the servers.

FYI - I'm unable to (cannot) install any modules (like Net::OpenSSH). Can we accomplish this with using something like ?

my @list_files = map { chomp; $_ } (`qx/ssh $serv_nme ls -t $d_loc/$wild_f_nme`);
1
  • 1
    If you can write .pl files, you can install modules. See here. Commented Aug 15, 2011 at 2:31

2 Answers 2

1

Have a look at GRID::Machine. It allows you to execute perl code on a remote machine and return perl data structures.

Something like this should work (untested):

use GRID::Machine;

my $machine = GRID::Machine->new(host => $host);

my $r = $machine->sub( 
  get_files=> q{
    my $pattern = shift;
    my @files = glob($pattern);
    # now stat each file name
    my %stats;
    for my $path (@files) {
      $stats{$path} = [ stat($path) ]
    }
    return \%stats;
  };
);

die $r->errmsg unless $r->ok;

$r = $machine->get_file_info($pattern);
my $stats = $r->Result;

# $stats is an array of hashes indexed by file name.

# $stats->{$path}->[7] is the file length of $path on the remote machine
Sign up to request clarification or add additional context in comments.

Comments

0

You can get the filenames somewhat smoother with glob:

@files = glob $wild_f_nme;

You can extract the digits in the date with a regex, e.g.;

my ($date) = $files[0] =~ /(\d{8})/;

I would use a date module to then find the most recent dates. Such as POSIX::strptime.

2 Comments

thanks for the response. But my main challenge is to ssh to remote servers and capture the file modify timestamps.
I see. I was confused by you asking how you could check time stamps. Using ssh in perl will be hard if you can't use modules. Assuming you do have ssh on your system, you could do a ssh command to execute some perl code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.