Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

user net:openSSH and File::find::Rule together.

by garcimo (Novice)
on Feb 15, 2018 at 14:52 UTC ( [id://1209226]=perlquestion: print w/replies, xml ) Need Help??

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

Hello I would like to search files in a remote system that match certain pattern that are older than one hour. the remote system is solaris. the find in solaris does not have parametres like mmtime. so I created this script that does more or less what i want in the remote system.
#!/usr/bin/perl use File::Find::Rule; use POSIX qw(strftime); my $today = time(); my $onehour = $today - (60*60); my @files = File::Find::Rule->file() ->name("*.0") ->mtime("<$onehour") ->in( "/mypath/" ); for my $file (@files) { print "$file\n"; }
now I want to use from a remote system using net:openssh so that it connects to the solaris with ssh finds the file and gives me the output..
use Net::OpenSSH; my $dir = '/mypath'; my $host = 'myhost'; my $ssh = Net::OpenSSH->new($host, user => adm_garcimo); $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error;
is there a way to merge the two script in one central server without having to scp the scripts to all the hosts that the script needs to find files... I hope imy question is clear

Replies are listed 'Best First'.
Re: user net:openSSH and File::find::Rule together.
by salva (Canon) on Feb 15, 2018 at 15:27 UTC
    There are several ways to do what you want:

    • Pass the code to the remote perl via stdin:
      my $out = $ssh->capture({stdin_data => <<'EOS'}, '/usr/bin/perl'); # Your remote script goes here: use File::Find::Rule; use POSIX qw(strftime); ... EOS
    • For more complex scripts, for instance requiring modules not installed in the remote machine, you can use Object::Remote, alone or from Net::OpenSSH.
    • You can access the remote file system using SFTP without running any script there. Net::OpenSSH is integrated with Net::SFTP::Foreign which provides a find method:
      my $sftp = $ssh->sftp; my $now = time; my @files = $sftp->find("/mypath", wanted => sub { my $e = $_[1]; $e->{filename} =~ /\.0$/ or return; $e->{a}->mtime + 3600 >= $now or return; 1 });
      Probably this method is going to be slower than the others as running a find operation over SFTP does lots of network roundtrips, but on the other hand you don't depend on having perl there, in some known place or programming for an antediluvian Perl version.

      Update: The capture method call was wrong. Corrected!

      thanks I prefer using the stdin_data route. somehow it is not doing it: this is the code:
      $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error; my $output = $ssh->capture(stdin_data => <<'EOS', '/usr/bin/perl'); use File::Find::Rule; my $today = time(); my $onehour = $today - (60*60); my @files = File::Find::Rule->file() ->name("*.0") ->mtime("<$onehour") ->in( "/mypath/" ); for my $file (@files) { print "$file\n"; } EOS
      the scripts outputs this: bash: line 15: stdin_data: command not found I do not understand what bash has to do here since we are full perl. thanks in advance for the coop.
        Oops, the options to capture have to be passed as a hash reference. I have corrected the code above.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-19 02:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found