vonedaddy has asked for the wisdom of the Perl Monks concerning the following question:

I have been playing with perl on and off for the last year or so, but I am in no way a programmer, so bare with me if I explain my problems wrong.

I am using expect to login and run some commands on remote servers. The problem I am having is there is a large SSH banner which seems to confuse expect. It says: SSHAuthenticationError Login timed out. The input stream currently has the contents bellow: ****************************************************************************** Then the banner here.

Second problem I am having is, I have using the following code to read the output from commands and print it to the screen. But it never seems to read the last line of output. I am wondering if its because the last line is delayed? Is there a function to make the read line statement wait for a prompt back?

my $line; while ( defined ($line = $ssh->read_line()) ) +{ print $line . "\n";
Thanks in advance.

Replies are listed 'Best First'.
Re: Net::SSH::Expect - Reading stream and banner troubles.
by kcott (Archbishop) on Oct 13, 2010 at 12:47 UTC

    The last line you are reading in probably doesn't have a terminator.

    The documenation for Net::SSH::Expect explains this: see the string read_line([$timeout]) ... section (under METHODS).

    -- Ken

Re: Net::SSH::Expect - Reading stream and banner troubles.
by Khen1950fx (Canon) on Oct 13, 2010 at 13:22 UTC
    There's a bug. readline() should be read_line(). You'll need a timeout also. This works:
    #!/usr/bin/perl use strict; use warnings; use Net::SSH::Expect; my $ssh = Net::SSH::Expect->new( host => 'localhost', user => 'user', password => 'password', timeout => 1, ); warn "Starting SSH..."; $ssh->run_ssh(); warn "Testing login output..."; my $login_output = $ssh->login(); print " Done", "\n"; my $who = $ssh->send('ls'); my $line; while ( defined ($line = $ssh->read_line()) ) { print $line . "\n"; }