0

I have a Perl script which is doing remote login to the Windows server via the Net::SSH::Perl module.

If I run the script manually from my shell, it gives no error. Running from a crontab, however, it connects properly to the remote Windows server but can't log in.

Here is the script:

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";

On server A, By running this script in csh shell I get output like :

Test script is started on server name
Connecting : * with user * and pwd *
Connected ...
Now ,Trying to loggin ,sever ip : *
Yeah , successfully logged in to *

In the shell, perl test.pl and sudo perl test.pl both work fine.

But running this script in cron as user root:

Test script is started on server name
Connecting : * with user * and pwd *
Connected ...
Now ,Trying to loggin ,sever ip : * 

Note: I am able to do this on another Linux server B with same file permissions, same username, and same password. The difference is that when I run this Perl script without sudo on server B, I am getting this error:

host key has changed! at /usr/local/share/perl5/Net/SSH/Perl/Kex/DH1.pm line 49

I don't see this error from Linux server A (the one on which I can't connect via cron)

What is wrong and how can I solve it?

4
  • You can enable debug when connecting, and eq is string operator but undef is not a string (use strict; use warnings;) Commented Jun 30, 2015 at 11:48
  • from host B, don't you have a offending key when connecting manually ? Commented Jun 30, 2015 at 14:03
  • Are you running the script as root from the comand line, or as some other user? Commented Jun 30, 2015 at 15:10
  • Len Jaffe - Manually means from command line, i have tried with root, sudo and i succeed but while running it from cron, i can do remote connection with module but can't be able to login ...!!! Commented Jul 3, 2015 at 9:56

1 Answer 1

1

It looks like host key verification is failing preventing the authentication. There may be host keys stored in ~/.ssh/known_hosts in the user's(root) home directory try deleting the remote host key from "known_hosts" and then re-try.

Sign up to request clarification or add additional context in comments.

3 Comments

CPAN doc for this module there is an option strict_host_key_checking This corresponds to the StrictHostKeyChecking ssh configuration option. Allowed values are no, yes, or ask. no disables host key checking, e.g., if you connect to a virtual host that answers to multiple IP addresses. yes or ask enable it, and when it fails in interactive mode, you are asked whether to continue. The host is then added to the list of known hosts.
my $ssh = Net::SSH::Perl->new($host,port=>$port,strict_host_key_checking=>no); if you don't need host key check, put 'yes' if known host check is required
chetangb, Thanks for suggestion . But i still can't solve the issue by doing the removing hostkey from ~/.ssh/known_hosts (root user) and also adding "strict_host_key_checking=>no" in Net::SSH::Perl module parameters .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.