Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Help with Net::OpenSSH::Compat::SSH2

by abetrwy (Initiate)
on Mar 14, 2013 at 19:20 UTC ( #1023537=perlquestion: print w/ replies, xml ) Need Help??
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; }

Comment on Help with Net::OpenSSH::Compat::SSH2
Download Code
Re: Help with Net::OpenSSH::Compat::SSH2
by kielstirling (Scribe) on Mar 15, 2013 at 00:13 UTC
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 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 (Monsignor) 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:

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2014-09-18 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (104 votes), past polls