Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by salva (Abbot)
on Mar 15, 2013 at 08:41 UTC ( #1023641=note: print w/ replies, xml ) Need Help??


in reply to Help with Net::OpenSSH::Compat::SSH2

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:

use strict; use warnings; use Data::Dumper; my %connection_details = ( host => 'localhost', @ARGV); my $SFTP = SFTP_connection(%connection_details); print "SFTP: ", Dumper($SFTP), "\n"; exit (0); BEGIN { eval { require Net::SSH2; warn "Net::SSH2 loaded"; 1; } or eval { require Net::OpenSSH::Compat::SSH2; Net::OpenSSH::Compat::SSH2->import(':supplant'); warn "Net::SSH2 supplanted"; 1; } or die "unable to load any SSH module: $@"; Net::SSH2->import(); } sub SFTP_connection { my (%connection_info) = @_; ## 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 Re: Help with Net::OpenSSH::Compat::SSH2
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2015-07-28 05:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (252 votes), past polls