我使用Net :: SSH :: Perl在远程服务器上执行命令,如下所示:
use Net::SSH::Perl;
my $ssh = Net::SSH::Perl->new($host);
$ssh->login($user, $pass);
my ($stdout, $stderr, $exit) = $ssh->cmd($cmd);
但是当我执行上面的脚本时,会再次出现密码提示。这意味着我在$ pass中提供的密码不用于建立ssh连接。为什么是这样?知道如何克服这个问题吗?我在下面给出调试信息:
serv1: Reading configuration data /root/.ssh/config serv1: Reading configuration data /etc/ssh_config serv1: Allocated local port 1023. serv1: Connecting to 15.154.59.63, port 22. serv1: Remote protocol version 1.99, remote software version OpenSSH_4.2 serv1: Net::SSH::Perl Version 1.34, protocol version 1.5. serv1: No compat match: OpenSSH_4.2. serv1: Connection established. serv1: Waiting for server public key. serv1: Received server public key (768 bits) and host key (1024 bits). serv1: Host '15.154.59.63' is known and matches the host key. serv1: Encryption type: DES3 serv1: Sent encrypted session key. serv1: Received encryption confirmation. serv1: RSA authentication failed: Can't load public key. serv1: Doing challenge response authentication. Password:
密码提示不应该是正确的,因为我已经在$ pass变量中提供了密码?
更新(根据收到的评论):
我尝试了以下内容:
my $ssh = Net::SSH::Perl->new($host, debug =>1, identity_files => []);
现在,我没有收到“RSA身份验证失败:”消息,但仍显示密码提示。我在下面给出调试信息:
serv1: Allocated local port 1023. serv1: Connecting to 15.154.59.63, port 22. serv1: Remote protocol version 1.99, remote software version OpenSSH_4.2 serv1: Net::SSH::Perl Version 1.34, protocol version 1.5. serv1: No compat match: OpenSSH_4.2. kf-linux-dm3: Connection established. serv1: Waiting for server public key. serv1: Received server public key (768 bits) and host key (1024 bits). serv1: Host '15.154.59.63' is known and matches the host key. serv1: Encryption type: DES3 serv1: Sent encrypted session key. serv1: Received encryption confirmation. serv1: Doing challenge response authentication. Password:
有人可以启发我吗?
答案 0 :(得分:5)
错误中的这一行:
serv1:RSA身份验证失败:无法加载公钥。
告诉我您的程序正在尝试执行automatic logins,这不是您在需要密码登录时想要执行的操作。
identity_files
要在RSA / DSA身份验证中使用的RSA / DSA身份文件列表。此参数的值应该是对字符串数组的引用,每个字符串标识标识文件的位置。将针对服务器测试每个身份文件,直到客户端找到成功进行身份验证的身份文件。
如果您不提供此功能,则RSA身份验证默认使用$ ENV {HOME} /。ssh / identity,而DSA身份验证默认为$ ENV {HOME} /。ssh / id_dsa。
您可以尝试将identity_files
设置为空数组,以查看是否强制它使用您提供的密码;或者您可以继续设置公钥认证,如上面的链接。
答案 1 :(得分:1)
serv1:进行质询响应认证。
这是你的关键路线。 SSH实际上有多种类型的类似密码的身份验证。其中一个是海峡密码验证(由PasswordAuthentication
中的/etc/sshd_config
选项控制),我怀疑这是您发送的$pass
的用途。另一个是challenge-response authentication,其中服务器发送一个或多个挑战,并且您应该回复正确的响应。 Ref和Spec。
所以,从技术上讲,ssh并没有要求你输入密码。它要求你回复来自服务器的任意挑战,而这个挑战恰好是“Password:
”。
如果您明确使用Net::SSH::Perl::KeyboardInt,它可能会起作用吗?
当然,你确实应该设置RSA公钥认证。
答案 2 :(得分:0)
您需要禁用ChallengeResponse身份验证:
#!/usr/bin/perl
use strict;
use Net::SSH::Perl;
my $user = 'user';
my $pw = 'pw';
my $host = 'some.server';
my $cmd = 'id';
my %params = (
'debug' => 0,
);
# get the ssh connection object
my $ssh = Net::SSH::Perl->new($host, %params);
# get the configuration object associated with the host connection
my $cfg = $ssh->config;
# disable challenge/response authentication
my $v = $cfg->set('auth_ch_res', 0);
# set the login parameters
$ssh->login($user, $pw);
# issue the command (which is when the authentication actually takes place)
my($stdout, $stderr, $exit) = $ssh->cmd($cmd);
# do what you want with the result
print $stdout;