Weird issue with Net::SSH::Expect in Perl script

别来无恙 提交于 2019-12-25 07:06:44

问题


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!


回答1:


I did end up resolving this. In the constructor for $ssh I set the timeout to 10 seconds, instead of the default 1. The script runs significantly slower, but I don't appear to have the issues I was running into before. Appreciate the feedback!




回答2:


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.



来源:https://stackoverflow.com/questions/18365492/weird-issue-with-netsshexpect-in-perl-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!