For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2008 > Net::SSH::Perl won't set SSH options?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Net::SSH::Perl won't set SSH options?
Richard Fernandez

2008-04-02, 7:19 pm

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 \@output;
}


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com