gpt4 book ai didi

Perl Net::SSH2 公钥认证问题

转载 作者:行者123 更新时间:2023-12-02 13:46:36 25 4
gpt4 key购买 nike

我正在尝试使用 Net::SSH2 连接到远程 SSH 服务器。命令行 ssh 工作正常。不过,我似乎无法找出正确的 auth_hostbased 参数

这是我的代码:

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 root@myhost',
'-----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;

该片段在 @ $ssh->auth_hostbased 中仅包含一个“Net::SSH2::DESTROY object 0xe17de0”。设置跟踪似乎无关紧要。用 $ssh->die_with_error 替换 die 会引发“die_with_error 不是有效的 Net::SSH2 宏”。下载当前 0.53 版本的 Net:SSH2 不起作用,因为脚本不再编译:'Net::SSH2 object version 0.44 does not match bootstrap parameter 0.53'

感谢任何有关正确参数格式或替代模块的帮助。

最佳答案

为什么不使用 Net::OpenSSH ?
这是一个简单的 ssh 包装脚本,我前段时间写过:

#!/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);
}

使用 cpanm (cpanm Net::OpenSSH) 或作为 debian 包“libnet-openssh-perl”安装它。
有关可用的主选项,请参阅“man ssh_config”。
我认为该脚本将有很大帮助。
Rgds。弗朗茨

关于Perl Net::SSH2 公钥认证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37503342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com