我有一个 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 $pass\n";
my $ssh = Net::SSH::Perl->new($host,port=>$port);
if ($ssh eq undef)
{
print "Can't connect to server $host\n";
exit;
}
print "Connected ... \nNow ,Trying to loggin ,sever ip : $host\n";
$ssh->login($user, $pass);
print "Yeah , successfully logged in to $host\n";
在服务器 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 *
在外壳中,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 上运行此 Perl 脚本时sudo
,我收到此错误:
host key has changed! at /usr/local/share/perl5/Net/SSH/Perl/Kex/DH1.pm line 49
我没有从 Linux 服务器 A 看到这个错误(我无法通过 cron 连接的那个)
出了什么问题,我该如何解决?