Hi folks,
 
I have a test script that remotely runs a command via SSH. The problem
is that I get prompted to accept a host key if it's missing from my
known_hosts file.
The ssh option StrictHostKeyChecking if set to 'no' is supposed to
accept a missing host key. I've tested this and it works from a command
line, but 
I can't get the following code to do the same. (I don't think the
ConnectTimeout works correctly either, FYI).
 
Can anyone see what I'm doing wrong?
 
Thanks!
 
richf
 
#!/usr/local/bin/perl 
use warnings;
use strict;
use Net::SSH::Perl;
 
my %config = (
        server              => 'server',
        passwords       => [qw(foo bar)],
        command        => 'who',
        user                => 'root',
);
 
my $output = collect_data_over_ssh(\%config);
print "\n\n", @$output, "\n\n";
 
sub collect_data_over_ssh {
        my $config = shift;
 
        my $server              = $config->{server};
        my $passwords   = $config->{passwords};         # array ref
        my $command             = $config->{command};
        my $user                = $config->{user};
 
        my @output;
        my $stdout;
        my $stderr;
        my $exit;
 
        my %options = (
                debug => 1,
                options => [
                        "ConnectTimeout 3",
                        "StrictHostKeyChecking no",
                ],
        );
 
 
        PASSWD:
        for my $pass (@$passwords) {
 
                my $ssh = Net::SSH::Perl->new($server, %options) or warn
"Can't connect via SSH $!\n";
 
                eval {
                        $ssh->login($user, $pass);
                };
                next PASSWD if ($@);
 
                ($stdout, $stderr, $exit) = $ssh->cmd($command);
 
                if ($stdout) {
                        @output = $stdout;
                }
                else {
                        @output = $stderr;
                }
                last PASSWD;
        }
        return [EMAIL PROTECTED];
}

Reply via email to