2016-05-28 12 views
5

Sto provando a connettermi a un server SSH remoto utilizzando Net :: SSH2. Commandline ssh funziona bene. Io non riesco a capire i parametri auth_hostbased corretti, anche sePerl Net :: Problema di autenticazione pubkey SSH2

Questo è il mio codice:

use Net::SSH2; 

my $ssh = Net::SSH2->new(); 
$ssh->debug(1); 
$ssh->trace(-1); 
$ssh->connect('remotehost.remotedomain.tld') or die; 
$ssh->auth_hostbased('username', 
    'ssh-rsa AAAAB3Nz[..]C0JoaFF9 [email protected]', 
    '-----BEGIN RSA PRIVATE KEY----- 
    Proc-Type: 4,ENCRYPTED 
    DEK-Info: AES-128-CBC,FA97214E87562096A7E480C82DAE5EB4 

    XIMKnj9k[..]kpRo5V 
    -----END RSA PRIVATE KEY-----', 
    'myhost.mydomain.tld', 
    'username', 
    'keypassword') or die; 

Il frammento muore @ $ ssh-> auth_hostbased con solo un 'Net :: :: SSH2 DISTRUGGERE oggetto 0xe17de0' . L'impostazione della traccia non sembra avere importanza. Sostituire die con $ ssh-> die_with_error genera un 'die_with_error non una macro Net :: SSH2 valida'. Download della versione 0.53 corrente di Net: SSH2 non ha funzionato in quanto lo script non compila più: 'Net :: SSH2 versione dell'oggetto 0.44 non corrisponde al parametro bootstrap 0.53'

Qualsiasi aiuto sul formato di parametro corretto o un modulo alternativo è apprezzato.

+1

si passa la passphrase come ultimo argomento, giusto? Quale modalità di autenticazione hai usato quando ti sei connesso dalla riga di comando? – SilentMonk

+2

Funziona in bash: 'ssh -i /root/.ssh/privkey [email protected] ls -l' e quindi inserisco keypassword per sbloccare privkey – Marcus

+1

Utilizzare il metodo 'auth_publickey'. BTW, ultima versione di Net :: SSH2 è 0,60. – salva

risposta

0

Perché non utilizzare Net :: OpenSSH? Questo è uno script SSH involucro semplice, ho scritto qualche tempo fa:

#!/usr/bin/perl 

#Simple SSH Remote Executor using Net::OpenSSH Library 

use warnings; 
use strict; 
use Net::OpenSSH; 
# see http://search.cpan.org/~salva/Net-OpenSSH-0.62/lib/Net/OpenSSH.pm#DEBUGGING 
$Net::OpenSSH::debug = undef; 
use Getopt::Long; 


my $timeout = 10; 
my ($username,$identity,$hostname,$command) = undef; 
my $uid=getpwuid($<); 
my $ctl_dir=qq{/tmp/.libnet-puppet-$uid}; 
my $ctl_mode=0700; 

if (! -d $ctl_dir) { mkdir($ctl_dir,$ctl_mode) }; 

open my $stderr_fh, '>>', '/dev/null' or die $!; 


sub print_help{ 
    print qq{\nusage: $0 [options] -h Hostname 

     -u username 

     -i identity 

     -c command 

     long options are supported ! 

    }; 
     exit (1); 
} 



GetOptions ("hostname=s" => \$hostname, # string 
       "username=s" => \$username, # string 
       "identity=s" => \$identity, # string 
       "command=s" => \$command) # string 
or print_help; 

if (not defined $username or not defined $identity or not defined $hostname or not defined $command) { print_help }; 

my $port = q{22}; 
my $user = $username; 
my $ssh; 

my $cmd = qq{$command}; 

my $options = { 
    host => $hostname, 
      user => $user, 
      port => $port, 
      default_stderr_fh => $stderr_fh, 
     ctl_dir => $ctl_dir, 
     master_opts => [ 
        -o => "UserKnownHostsFile=/dev/null", 
        -o => "StrictHostKeyChecking=no", 
        -o => qq{IdentityFile=$identity}, 
       ], 
    timeout => $timeout }; 

#ALARM Timer timeout handling 
$SIG{ALRM} = sub { 
    printf("%s\n", qq{invalid-timeout-connecting-to-node-$hostname}); 
    exit(1); 
}; 

#init alarm timer ;-) 
alarm($timeout); 

$ssh = Net::OpenSSH->new(%{$options}) 
      or $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error; 

my (@out, $err) = $ssh->capture2({ timeout => 10 }, $cmd); 

die("Error: %s\n", $err) if defined $err; 

if ((scalar(@out)) eq 0) { 
    printf("%s\n", qq{invalid-empty-string-received-by-node-$hostname}); 
    exit(1); 
} 

foreach my $line (@out) { 
    $line =~ s/^\s{1,}//; 
     printf ("%s",$line); 
} 

installarlo utilizzando cpanm (cpanm Net :: OpenSSH) o come pacchetto debian "libnet-openssh-perl".
Vedere "man ssh_config" per le opzioni principali disponibili.
Penso che la sceneggiatura sarà di grande aiuto.
Rgds. Franz

Problemi correlati