私はperlスクリプトをまとめることに取り組んでいます。以下にキャプチャしました。
#!/usr/bin/perl
use Tie::File;
use Net::SSH::Expect;
use utf8;
use warnings;
use diagnostics;
# Grab password from hidden file
$pw=`cat .password`;
chomp $pw;
#Read list of 9200's from hosts.list file into an array
tie @hosts, 'Tie::File', "hosts.list" or die;
#Loop through hosts, connect via ssh, run commands, and write out log files.
foreach (@hosts) {
#Create ssh session handle
my $ssh = Net::SSH::Expect->new (
host => $_,
password => $pw,
user => 'user',
raw_pty => 1
);
my $login_output = $ssh->login();
if ($login_output !~ /.*sbc.*>/) {
die "Login failed. Login output was $login_output";
}
$ssh->send("show sip errors");
my $line;
while ( defined ($line = $ssh->read_line()) ){
print $line . "\n";
}
$ssh->close();
}
まず、私はプログラマーではないので、スタイルはおそらく非常に醜いです。申し訳ありません:) 目標は、リモートアプライアンスでいくつかのコマンドを実行し、結果を個別のファイルにキャプチャして、サードパーティの解析エンジン (splunk) によって消費されることです。
現在実装されている機能は、リモート ホストにログインし、コマンドを実行して、stdout に出力できます。そこまでではありませんが、それでも良い概念実証を示しています。
このスクリプトは、hosts.list ファイルの最初の 3 つのホストに対して正常に実行されます。ただし、4 番目のホストに到達するとすぐに、次の例外が発生します。
Uncaught exception from user code:
SSHAuthenticationError Login timed out. The input stream currently has the contents bellow: user@myhost.mydomain's password: at /System/Library/Perl/Extras/5.12/Expect.pm line 828
at /Library/Perl/5.12/Net/SSH/Expect.pm line 209
Net::SSH::Expect::__ANON__('ARRAY(0x7fd718a03008)') called at /System/Library/Perl/Extras/5.12/Expect.pm line 828
Expect::_multi_expect(1, 'ARRAY(0x7fd7189fbce8)', 'ARRAY(0x7fd7189f7460)') called at /System/Library/Perl/Extras/5.12/Expect.pm line 565
Expect::expect('Expect=GLOB(0x7fd7189f1878)', 1, 'ARRAY(0x7fd718a01530)', 'ARRAY(0x7fd7189f15a8)', 'ARRAY(0x7fd71890a3d0)', 'ARRAY(0x7fd718a07470)', 'ARRAY(0x7fd7189d8b18)') called at /Library/Perl/5.12/Net/SSH/Expect.pm line 580
Net::SSH::Expect::_sec_expect('Net::SSH::Expect=HASH(0x7fd718a29828)', 1, 'ARRAY(0x7fd718a01530)', 'ARRAY(0x7fd7189f15a8)', 'ARRAY(0x7fd71890a3d0)', 'ARRAY(0x7fd718a07470)', 'ARRAY(0x7fd7189d8b18)') called at /Library/Perl/5.12/Net/SSH/Expect.pm line 213
Net::SSH::Expect::login('Net::SSH::Expect=HASH(0x7fd718a29828)') called at ./pcscfFetch.pl line 26
問題が何であるかについてのアイデアはありますか?ssh 経由で手動で問題なくホストにログインできます。このスクリプトは、他のホストでも問題なく動作します。私が理解できないのは、この 1 つの外れ値だけです。アドバイスをいただければ幸いです。ありがとう!