Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by salva (Canon)
on Feb 15, 2018 at 15:27 UTC ( [id://1209229]=note: print w/replies, xml ) Need Help??


in reply to user net:openSSH and File::find::Rule together.

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!

Replies are listed 'Best First'.
Re^2: user net:openSSH and File::find::Rule together.
by garcimo (Novice) on Feb 15, 2018 at 17:12 UTC
    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.

        could the contents of the data feed by stdin_data we put in a subroutine instead..

        I tried to put it in a subroutine like this:
        sub finder { use File::Find::Rule; my $today = time(); my $onehour = $today - (60*60); my @files = File::Find::Rule->file() ->name("*.0") ->mtime("<$onehour") ->in( "/export/home/adm_garcimo" ); for my $file (@files) { return $file; } }
        then call the subroutine:
        my $output = $ssh->capture({stdin_data => <<'EOS'}, '/usr/bin/perl'); finder() EOS print $output
        but it does not find the subroutine..

        thanks again.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1209229]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found