Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

OpenSSH capture perl-command

by Alasharin (Initiate)
on Mar 14, 2014 at 16:01 UTC ( #1078355=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on OpenSSH capture perl-command
Select or Download Code
Re: OpenSSH capture perl-command
by salva (Monsignor) on Mar 15, 2014 at 07:16 UTC
    The command argument to Net::OpenSSH methods must be a system command (e.g. ls, grep, etc.). You can not pass perl code there.

    In your particular case, you can use SFTP to query the remote filesystem:

    use Data::Dumper; my $sftp = $ssh->sftp; my $stat = $sftp->stat($remote_file); print Dumper($stat), "\n";

      ....Thanks...
      Regards, Armin </>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1078355]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-10-25 02:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (140 votes), past polls