http://www.perlmonks.org?node_id=1078355

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

Hi all,
i'm searching for a solution / or any ideas to get a better working to the epochal mtime of remote files to compare them and after comparing copy only the youngest file to local-dir. Any ideas?

I've tried like this ... but it doesn't work:

#!/usr/bin/perl -w use strict; use warnings; use lib "/opt/perl_addmodules/lib/site_perl"; use Net::OpenSSH; use Data::Dumper; ######################################################## my (%ssh, $evalh3, $evalh4, $acthost,$ssha); ######################################################## my $host4 = 'foo'; my $host3 = 'bar'; my @hosts = ($host3,$host4); my @files = ('file1.csv','file2.csv','file3.csv'); my $realrun = 'n'; my $user = 'root'; my $localdir = '/.../'; my $localbakdir = '/.../.../'; my $remotedir = '/.../'; my $reffile = $remotedir.$files[0]; my $tmpdir = '/tmp/'; my $evalfile = '_eval'; # ####################### main ###################### if ($realrun eq 'n') { print "Main: Simulate-Run: .................... \n"; } # Make Multiple-Master-ssh Connections for my $host (@hosts) { $ssh{$host} = Net::OpenSSH->new($user.'@'.$host, async =>1); $ssh{$host}->error and warn "Couldn't establish SSH connection $ho +st: ". $ssh{$host}->error; print " --------------$host-------------- \n"; if ($host eq $host3){ $evalh3 = $ssh{$host}->capture((stat("$reffile"))[9]); } # This does +n't work !! if ($host eq $host4){ $evalh4 = $ssh{$host}->capture((stat("$reffile"))[9]); } # This does +n't work !! } # end for if ($evalh3 > $evalh4) { $acthost = $host3; } else { $acthost = $host4; } $ssha = Net::OpenSSH->new($user.'@'.$acthost); $ssha->error and warn "Couldn't establish SSH connection: ". $ssha->er +ror; foreach (@files) { if (-e $localdir.$_) { print "Main: File: $_ exist and will be moved to $localbakdir +\n"; if ($realrun eq 'y') { move($localdir.$_, $localbakdir.$_); $ssha->scp_get({quiet=>0, copy_attrs=>1}, $remotedir.$_, $ +localdir.$_); } else { print "Main: Simulate-Run: $_ to copy from $acthost... +................. \n"; } } else { print "Main: File: $_ does not exist \n"; } } #end foreach

.... This was what i want ... but...maybe... someone has an idea to get it work - perhaps with expect?

...And for completeness: here is my workaround ...but i hope there is a better way...:

#!/usr/bin/perl -w use strict; use warnings; use lib "/opt/perl_addmodules/lib/site_perl"; use Net::OpenSSH; use File::Copy; use File::Basename; use Data::Dumper; use POSIX qw(setuid); ######################################################## my (%ssh, $evalh3, $evalh4, $acthost,$ssha); ######################################################## my $host4 = 'foo'; my $host3 = 'bar'; my @hosts = ($host3,$host4); my @files = ('file1.csv','file2.csv','file13.csv'); my $realrun = 'n'; my $user = 'root'; my $localdir = '/usr/.../...../'; my $localbakdir = '/usr/.../........./'; my $remotedir = '/usr/.../......./'; my $reffile = $remotedir.$files[0]; my $tmpdir = '/tmp/'; my $evalfile = '_eval'; # ####################### main ###################### if ($realrun eq 'n') { print "Main: Simulate-Run: .................... \n"; } # Make Multiple-Master-ssh Connections for my $host (@hosts) { $ssh{$host} = Net::OpenSSH->new($user.'@'.$host, async =>1); $ssh{$host}->error and warn "Couldn't establish SSH connection $ho +st: ". $ssh{$host}->error; print " --------------$host-------------- \n"; $ssh{$host}->scp_get({quiet=>0, copy_attrs=>1 }, $reffile, $tmpdir +.$host.$evalfile); } # end for if (-e $tmpdir.$host3.$evalfile) { $evalh3 = (stat("$tmpdir$host3$evalfile"))[9]; } else { $evalh3 = 0; } if (-e $tmpdir.$host4.$evalfile) { $evalh4 = (stat("$tmpdir$host4$evalfile"))[9]; } else { $evalh4 = 0; } print "$host3: ... FileAge: ... $evalh3 \n"; print "$host4: ... FileAge: ... $evalh4 \n"; if ($evalh3 > $evalh4) { $acthost = $host3; } else { $acthost = $host4; } print " --------------$acthost-------------- \n"; $ssha = Net::OpenSSH->new($user.'@'.$acthost); $ssha->error and warn "Couldn't establish SSH connection: ". $ssha->er +ror; foreach (@files) { if (-e $localdir.$_) { print "Main: File: $_ exist and will be moved to $localbakdir +\n"; if ($realrun eq 'y') { move($localdir.$_, $localbakdir.$_); $ssha->scp_get({quiet=>0, copy_attrs=>1}, $remotedir.$_, $ +localdir.$_); } else { print "Main: Simulate-Run: $_ to copy from $acthost... +................. \n"; } } else { print "Main: File: $_ does not exist \n"; } } #end foreach unlink $tmpdir.$host3.$evalfile; unlink $tmpdir.$host4.$evalfile;

Thanks and regards Armin