我有一个Perl脚本,它通过Net::SSH::Perl
模块远程登录到Windows服务器。
如果我从shell手动运行脚本,它不会出错。然而,从crontab运行时,它可以正确连接到远程Windows服务器,但无法登录
这是脚本:
use Net::SSH::Perl;
use Date::Calc qw(:all);
use DateTime::Format::Epoch;
$lin_host = `hostname -s`;
chomp($lin_host);
print "Test script is started on server $lin_host at time ",join(":",Now()),"n";
$host = "any valid ip";
$user = "valid username";
$pass = "valid password";
$port = "valid port";
if ($ARGV[0] eq "eodupdate")
{
$host = "valid host";
}
print "Connecting : $host with user $user and pwd $passn";
my $ssh = Net::SSH::Perl->new($host,port=>$port);
if ($ssh eq undef)
{
print "Can't connect to server $hostn";
exit;
}
print "Connected ... nNow ,Trying to loggin ,sever ip : $hostn";
$ssh->login($user, $pass);
print "Yeah , successfully logged in to $hostn";
在服务器A上,通过在csh-shell中运行此脚本,我得到的输出如下:
Test script is started on server name
Connecting : * with user * and pwd *
Connected ...
Now ,Trying to loggin ,sever ip : *
Yeah , successfully logged in to *
在shell中,perl test.pl
和sudo perl test.pl
都工作良好。
但是以root用户身份在cron中运行此脚本:
Test script is started on server name
Connecting : * with user * and pwd *
Connected ...
Now ,Trying to loggin ,sever ip : *
注意:我可以在具有相同文件权限、相同用户名和相同密码的另一台Linux服务器B上执行此操作。不同的是,当我在服务器B上运行这个没有sudo
的Perl脚本时,我会得到以下错误:
host key has changed! at /usr/local/share/perl5/Net/SSH/Perl/Kex/DH1.pm line 49
我没有从Linux服务器A(我无法通过cron连接的服务器)中看到这个错误
哪里出了问题,我该如何解决?
主机密钥验证似乎失败,无法进行身份验证。可能有主机密钥存储在用户(根)主目录的~/.ssh/non_hosts中。请尝试从"known_hosts"中删除远程主机密钥,然后重试。