Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

instantiating an SFTP object

by Aldebaran (Curate)
on Jun 09, 2017 at 22:11 UTC ( [id://1192447]=perlquestion: print w/replies, xml ) Need Help??

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

I have a templating system that I use to create pages on my site. Recently, in talking with their tech support, we switched from normal ftp to sftp. Ever since, the code I had been using has been essentially broken. What I had before was:

sub get_ftp_object{ use strict; use Net::FTP; my $sub_hash = "my_ftp"; my $domain = $config{$sub_hash}->{'domain'}; my $username = $config{$sub_hash}->{'username'}; my $password = $config{$sub_hash}->{'password'}; #dial up the server my $ftp = Net::FTP->new( $domain, Debug => 0, Passive => 1 ) or die "Can't connect: $!\n"; $ftp->login( $username, $password) or die "Couldn't login\n"; return $ftp; }

So, I think, gosh, all I have to do is append a cap S in front of FTP, download the module, and voila. There's always a tricky part, and this one was getting Math::GMP squared away, along with the requisite C library. For others who might trod this path--including myself the next time I get hung up on it, the following command for a debian install put gmp.h where it had to be:

sudo apt-get install libgmp3-dev

What I have now gets past the use statement but not by far:

Permission denied at /usr/local/share/perl/5.22.1/Net/SFTP.pm line 62.

This would be close to an analog of what I had. The say statement prints out the values that I have verified with my ISP. BTW, they think there is no reason I should not be able to transfer files.

sub get_ftp_object{ use strict; use Net::SFTP; 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'}; #dial up the server say "values are $domain $username $password"; my $sftp = Net::SFTP->new( $domain, Debug => 1) or die "Can't connect: $!\n"; $sftp->login( $username, $password) or die "Couldn't login\n"; return $sftp; }

It might help the local diagnosticians to show line 62 of SFTP.pm in context. I've remarked next to it and wonder if SFTP requires more to ask a server for a new object than FTP did:

# returns the new object sub init { my $sftp = shift; my %param = @_; $sftp->{debug} = delete $param{debug}; $sftp->{status} = SSH2_FX_OK; $param{ssh_args} ||= []; $param{ssh_args} = [%{$param{ssh_args}}] if UNIVERSAL::isa($param{ssh_args},'HASH'); $param{warn} = 1 if not defined $param{warn}; # default $sftp->{warn_h} = delete $param{warn} || sub {}; # false => ignor +e $sftp->{warn_h} = sub { carp $_[1] } # true => emit warning if $sftp->{warn_h} and not ref $sftp->{warn_h}; $sftp->{_msg_id} = 0; my $ssh = Net::SSH::Perl->new($sftp->{host}, protocol => 2, debug => $sftp->{debug}, @{ $param{ssh_args} }); $ssh->login($param{user}, $param{password}, 'supress_shell'); #lin +e 62 $sftp->{ssh} = $ssh; my $channel = $sftp->_open_channel; $sftp->{channel} = $channel; $sftp->do_init; $sftp; }

Alright, so there it is. I'm fishing for tips, tricks, whatever you have that pertains to getting SFTP working. It's really putting a cramp in my business to not be able to use my own site effectively. Thank you for your comment.

Replies are listed 'Best First'.
Re: instantiating an SFTP object
by runrig (Abbot) on Jun 09, 2017 at 22:33 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);
Re: instantiating an SFTP object
by zentara (Archbishop) on Jun 10, 2017 at 12:49 UTC
    so there it is. I'm fishing for tips, tricks, whatever you have that pertains to getting SFTP working.

    I have to say, I always have had good results with Net::SSH2, which contains an sftp client. See A little demo for Net::SSH2, which may be a little outdated, but it should show you basic usage.


    I'm not really a human, but I play one on earth. ..... an animated JAPH
        Ok, I've heard reports that people have had problems with it, but I didn't experience any. You asked for any ideas regarding sftp, so now you are wiser, maybe me too. :-)

        I'm not really a human, but I play one on earth. ..... an animated JAPH

      Thanks for your response, zentara . After a third time with a failed login using different methods that seem to work for everyone else, I double-checked my password, which hadn't been updated in my little world of perl development. (sorry about that: I'd roll my eyes if my mom did that) That solves one problem, but as I read, I come closer to the opinion that the preferred method for login is to use RSA key-pairs. So while I want to get some minimal functionality for getting content to my site, I'd also like to update this capability to reflect the methods of grown-ups using encryption.

      In order to install Net::SSH2 I had to run the following commands on a debian system:

      sudo apt-get install libssh2-1-dev sudo apt-get install zlib1g sudo apt-get install zlib1g-dev

      http://www.perlmonks.org/?node_id=569657 was very helpful. Unfortunately, http://cfm.gs.washington.edu/security/ssh/client-pkauth/ gets a 404 from my browser. I looked at https://www.debian.org/devel/passwordlessssh as a source for how I might go forward.

      As I look at what's in front of me, I have a machine capable of both SSH1 and SSH2. My ISP makes no distinction. Do I want to use one or the other? Does SSH2 comprehend SSH1? If I write for SSH2, will I have a wider range of application or more likely burdened by a level of encryption that hardly need exist for my little html pages that have nothing to do with banking, or national security and the like?

      Anyways, here's caller and sub on the what works now.

      #!/usr/bin/perl -w use strict; use 5.010; use lib "template_stuff"; use File::Basename; use Net::SSH2; use File::Spec; use Term::ReadKey; my $rftp = get_ftp_object(); say "object created, back in main";
      sub get_ftp_object{ use strict; use Net::SSH2; 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'}; say "values are $domain $username $password"; #dial up the server my $ssh2 = Net::SSH2->new(); $ssh2->connect($domain) or die "Unable to connect Host $@ \n"; say "connect worked"; #this works for passwords $ssh2->auth_password($username,$password) or die "Unable to login $@ \ +n"; return $ssh2; }

        Hi, if you read the demo a little more closely, you would see the section:
        # works when run from z's homedir because you need # permission to read the keys $ssh2->auth_publickey('z', '/home/z/.ssh/id_dsa.pub', '/home/z/.ssh/id_dsa', $pass );
        Try reading the latest docs for Net::SSH2. maybe that syntax has changed a bit. $pass is the password for your ssh key, not the login account

        P.S. use ssh2, ssh1 is very outdated


        I'm not really a human, but I play one on earth. ..... an animated JAPH
        Hi, here is a Net::SSH2 sftp script that works. I just verified it. This shows how to do a login with rsa keys, the password has been changed of course. Works on latest Slackware linux, which is pretty standard generic linux.
        #!/usr/bin/perl use warnings; use strict; use Net::SSH2 qw(LIBSSH2_CHANNEL_EXTENDED_DATA_MERGE LIBSSH2_CHANNEL_FLUSH_ALL LIBSSH2_HOSTKEY_POLICY_ASK); my $pass = 'rumpelstiltskin'; my $ssh2 = Net::SSH2->new( debug => 1 ); $ssh2->trace(-1); $ssh2->timeout(5000); $ssh2->connect('my.net') or $ssh2->die_with_error; $ssh2->auth_publickey('me', '/home/me/.ssh/id_rsa.pub', '/home/me/.ssh/id_rsa', $pass ); my $sftp = $ssh2->sftp(); my $fh = $sftp->open('/etc/passwd') or $sftp->die_with_error; print $_ while <$fh>; return 0; __END__

        I'm not really a human, but I play one on earth. ..... an animated JAPH

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-18 23:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found