我正在努力整理一个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个主机。但是,只要它到达第四个主机,我就会收到此异常:
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手动登录主机,没有任何问题。该脚本适用于我们的其他主机,它只是这一个异常值,我似乎无法弄清楚。任何意见,将不胜感激。谢谢!
答案 0 :(得分:0)
我最终解决了这个问题。在$ ssh的构造函数中,我将超时设置为10秒,而不是默认值1.脚本运行速度明显较慢,但我似乎没有遇到过之前遇到的问题。感谢您的反馈!
答案 1 :(得分:0)
Net :: SSH :: Expect不可靠。
请改用Net :: OpenSSH,或者如果要在多个主机Net :: OpenSSH :: Parallel中运行相同的命令集。