Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: instantiating an SFTP object

by runrig (Abbot)
on Jun 09, 2017 at 22:33 UTC ( #1192448=note: print w/replies, xml ) Need Help??

in reply to instantiating an SFTP object

I would go with Net::SFTP::Foreign.

Replies are listed 'Best First'.
Re^2: instantiating an SFTP object
by Aldebaran (Deacon) on Jun 10, 2017 at 06:54 UTC

    Thanks for the suggestion. This module seems to have a lot of functionality, so I definitely thought the try worth the effort. I'm calling it differently now and get a different error:

    Permission denied (publickey,password). object created, back in main
    sub get_ftp_object{ use strict; use Net::SFTP::Foreign; use 5.01; my $sub_hash = "my_sftp"; my $domain = $config{$sub_hash}->{'domain'}; my $username = $config{$sub_hash}->{'username'}; my $password = $config{$sub_hash}->{'password'}; my $port = 22; #dial up the server say "values are $domain $username $password"; my $sftp = Net::SFTP::Foreign->new( $domain, user => $username, port => $port, password => $password) or die "Can't connect: $!\n"; return $sftp; }

    I seem to have gotten farther but don't understand why I didn't die here, as execution goes back to main. (?)

      Net::SFTP::Foreign, by default, never dies when some error happens. Instead it keeps an internal error status that can be queried using the error method. For instance:
      $s = Net::SFTP::Foreign->new(...); if ($s->error) { die "Unable to connect to remote host: " . $s->error +}
      Alternatively, you can activate the autodie mode. It makes the object die, when some error happens:
      $s = Net::SFTP::Foreign->new(..., autodie => 1);

        I finally got my little template working with this module. I found all the discussion and example code necessary, including cpan paragraph

        I'm trying to use github to post bulky modules. author's github repo is the greater context in which this works:

        sub get_html_filename{ use Net::SFTP::Foreign; use File::Basename; use Cwd; use 5.01; my $sftp = shift; # get working directory my $dir = getcwd(); my $word = basename($dir); say "word is $word"; # get files from /pages my $ls = $sftp->ls("/pages", wanted => qr/$word/) or warn "unable to retrieve ".$sftp->error; print "$_->{filename}\n" for (@$ls); my @remote_files = map { $_->{filename} } @$ls; say "files are @remote_files"; my $rref = \@remote_files; my $filetype = "html"; my $old_num = highest_number($rref, $filetype, $word); print "old num is $old_num\n"; my $new_num = $old_num + 1; my $html_file = $word.$new_num.'.'.$filetype; return $html_file; }

        Abridged output from stdout:

        $ perl title is powell values are [correct] object created, back in main word is powell powell1.html powell2.html files are powell1.html powell2.html old num is 2 new file is powell3.html $

        The page, populated by mundane matters, accomplishes its humble task: page posted (no porn). How to dereference a reference to an array of hashrefs is something that runrig posted on 2013 node, to wit:

        my $ls = $sftp->ls('/home/ftptest/inbound', no_wanted => qr/^\./ ); my @names_and_sizes = map { { filename => $_->{filename}, size => $_-> +{a}->size } } @$ls; # or... my %size = map { $_->{filename} => $_->{a}->size } @$ls;

        To shoehorn this into an array of filenames, I turned this into:

        my @remote_files = map  { $_->{filename} } @$ls;

        and I was back in business. Thanks all for comments and code.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2021-04-11 21:09 GMT
Find Nodes?
    Voting Booth?

    No recent polls found