http://www.perlmonks.org?node_id=1023537

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

Need to send a file after I run a nightly cron to another server via SFTP/SSH. After doing a lot of reading here and other places I decided to forgo Net::SSH2 or Net::SFTP or Net::OpenSSH and went with: Net::OpenSSH::Compat::SSH2

It seems debug does not work. Anyway, I get a disconnect error at ->auth_password(). But, I can use Eclipse via ssh and sftp and login, I can use Winscp as well with the same credentials, so I know they are correct.

I am on - Ubuntu 12.04.1 LTS and I have OpenSSH_5.9p1 Debian-5ubuntu1, OpenSSL 1.0.1 14 Mar 2012

%connection_details = ( host => '<IP>', user => '<USER>', pass => '<PASS>', port => '<PORT>', ); $SFTP = &SFTP_connection(%connection_details); exit (0); sub SFTP_connection { my (%connection_info) = @_; use Net::OpenSSH::Compat::SSH2 qw(:supplant); use Net::SSH2; eval { require Net::SSH2; }; if ($@) { print "The Net::SSH2 or Net::OpenSSH::Compat::SSH2 Perl module + is not installed on this server, which is required for this feature. +\n Please contact your server administrator and ask them t +o install this module.\n"; return 0; } ## Connect to the sFTP server my $SFTP = Net::SSH2->new(); $SFTP->connect($connection_info{'host'},22) or die "Unable to connect to the remote sFTP server \n\n $@"; ## Login to sFTP server $SFTP->auth_password($connection_info{'user'},$connection_info{'pa +ss'}) or die "Unable to login Check username and password. \n\n $@\n +"; return $SFTP; }

Replies are listed 'Best First'.
Re: Help with Net::OpenSSH::Compat::SSH2
by 7stud (Deacon) on Mar 15, 2013 at 03:31 UTC

    On a side note,

    use Net::SSH2; eval { require Net::SSH2; }; if ($@) )

    Your use statement is equivalent to:

    BEGIN { require Net::SSH2; Net::SSH2->import(); }

    ...which executes at compile time. So your eval() happens after the party is over.

Re: Help with Net::OpenSSH::Compat::SSH2
by salva (Canon) on Mar 15, 2013 at 08:41 UTC
    use statments are executed at compile time, even if you define them inside the body of a function.

    In order to load Net::SSH2 or when not available, Net::OpenSSH::Compat::SSH2, this code should work:

    BEGIN { eval { require Net::SSH2; 1 } or eval { require Net::OpenSSH::Compat::SSH2; Net::OpenSSH::Compat::SSH2->import(':supplant'); 1; } or die "unable to load any SSH module"; Net::SSH2->import(); }

    Though, you would be better using Net::SSH::Any that takes care of doing all this conditional loading for you and provides a better API. It is still a work in progress, but basic functionality like running commands remotely and capturing the output already works fine.

    update: the full script fixed:

Re: Help with Net::OpenSSH::Compat::SSH2
by Illuminatus (Curate) on Mar 15, 2013 at 00:26 UTC
    You say that you chose this over several other packages:
    1. reason? I can understand only having OpenSSH to work with, but why not use Net::OpenSSH?
    2. you did see on the cpan doc the 'this is a work in progress' (in bold type)?
    That said, I would print out the name and password just before the call to auth_password to ensure they're what you think they are.

    fnord

Re: Help with Net::OpenSSH::Compat::SSH2
by kielstirling (Scribe) on Mar 15, 2013 at 00:13 UTC