Net :: SSH :: Expect在Perl脚本中的奇怪问题 [英] Weird issue with Net::SSH::Expect in Perl script

查看:321
本文介绍了Net :: SSH :: Expect在Perl脚本中的奇怪问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个Perl脚本。我已经抓住了它:

 #!/ usr / bin / perl 
使用Tie :: File;
使用Net :: SSH :: Expect;
使用utf8;
使用警告;
使用诊断;

#从隐藏文件中获取密码
$ pw =`cat .password`;
chomp $ pw;

#从hosts.list文件中读取9200的列表到数组
tie @hosts,'Tie :: File',hosts.list或者die;

#通过主机执行,通过ssh连接,运行命令和写出日志文件。

foreach(@hosts){

#Create ssh会话句柄
我的$ ssh = Net :: SSH :: Expect-> new(
host => $ _,
password => $ pw,
user =>'user',
raw_pty => 1
);
我的$ login_output = $ ssh-> login();
if($ login_output!〜/.*sbc.*>/){
die登录失败登录输出是$ login_output;
}
$ ssh-> send(show sip errors);
我的$行
while(defined($ line = $ ssh-> read_line())){
print $ line。 \\\
;
}
$ ssh-> close();
}

首先,我不是程序员,所以风格可能非常丑陋。对不起:)目标是在远程设备上运行几个命令,将结果捕获在单独的文件中,然后由第三方解析引擎(splunk)使用。



当前实现的功能能够登录到远程主机,运行命令,然后打印输出到stdout。不完整的,但仍然显示出一个很好的概念证明。



该脚本在hosts.list文件中的前3个主机运行正常。但是一旦到达第四个主机,我收到这个例外:

 用户代码未捕获的异常:
SSHAuthenticationError登录超时。输入流当前具有以下内容:user@myhost.mydomain的密码:/System/Library/Perl/Extras/5.12/Expect.pm行828
at /Library/Perl/5.12/Net/SSH/Expect .pm line 209
Net :: SSH :: Expect :: __ ANON __('ARRAY(0x7fd718a03008)')在/System/Library/Perl/Extras/5.12/Expect.pm行828
预期: :_multi_expect(1,'ARRAY(0x7fd7189fbce8)','ARRAY(0x7fd7189f7460)')在/System/Library/Perl/Extras/5.12/Expect.pm行调用565
Expect :: expect('Expect = GLOB (0x7fd7189f1878)',1,在/Library/Perl/5.12中调用的'ARRAY(0x7fd718a01530)','ARRAY(0x7fd7189f15a8)','ARRAY(0x7fd718a07470)','ARRAY(0x7fd7189d8b18)') /Net/SSH/Expect.pm line 580
Net :: SSH :: Expect :: _ sec_expect('Net :: SSH :: Expect = HASH(0x7fd718a29828)',1,'ARRAY(0x7fd718a01530)''在/ Library / Perl / 5中调用的ARRAY(0x7fd7189f15a8)','ARRAY(0x7fd71890a3d0)','ARRAY(0x7fd718a07470)','ARRAY(0x7fd7189d8b18)') 12 / Net / SSH / Expect.pm line 213
Net :: SSH :: Expect :: login('Net :: SSH :: Expect = HASH(0x7fd718a29828)')在./pcscfFetch.pl第26行调用

有什么问题可以解决的想法?我可以通过ssh手动登录到主机没有问题。该脚本对我们的其他主机工作正常,这只是一个异常值,我似乎无法弄清楚。任何建议将不胜感激。谢谢!

解决方案

Net :: SSH :: Expect不可靠。



使用Net :: OpenSSH,或者如果要在多个主机Net :: OpenSSH :: Parallel中运行相同的命令集。


I am working on putting together a perl script. I have captured it below:

#!/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();
}

First, I'm not a programmer, so style is probably very ugly. Sorry about that :) The goal is to run several commands on a remote appliance, capture the results in separate files, which will then be consumed by a 3rd party parsing engine (splunk).

The current implemented functionality is able to log in to remote hosts, run the command, and then print out to stdout. Not quite there, but still shows a good proof of concept.

The script runs fine for the first 3 hosts in the hosts.list file. However as soon as it gets to the fourth host, I receive this exception:

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

Any ideas on what the problem could be? I am able to log in to the host with no issue manually via ssh. The script works fine for our other hosts, it's just this one outlier that I can't seem to figure out. Any advice would be appreciated. Thanks!

解决方案

Net::SSH::Expect is not reliable.

Use Net::OpenSSH instead, or if you want to run the same set of commands in several hosts Net::OpenSSH::Parallel.

这篇关于Net :: SSH :: Expect在Perl脚本中的奇怪问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆