Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

EXPORT and Net::Openssh

by th'timmay (Initiate)
on Apr 18, 2013 at 18:45 UTC ( #1029405=perlquestion: print w/replies, xml ) Need Help??
th'timmay has asked for the wisdom of the Perl Monks concerning the following question:

i humbly submit my request for a nugget of wisdom o wise monks.

i'm trying to export some variables from a module into another, and that seems to be working as expected, however, net::openssh fails to establish a connection using the imported ones. if i declare the variables locally, everything works fine.
#!/usr/local/perl package Creds; use warnings; use strict; require Exporter; our @ISA = 'Exporter'; our @EXPORT_OK = qw($user $pass $enpass); our $user = '....'; our $pass = '....'; our $enpass = '....';
#! /usr/bin/perl use strict; use warnings; use Net::OpenSSH; use Expect; use IO::Stty; use Creds qw($user $pass $enpass); my $host = $ARGV[0]; my $timeout = 5; my $ssh = Net::OpenSSH->new("$user:$pass\@$host", timeout => 10); $ssh->error and die "unable to connect to host: ". $ssh->error; my ($pty, $pid) = $ssh->open2pty(); my $expect = Expect->init($pty); $expect->expect($timeout, [ '>' => sub { $expect->send('enable'); $expect->send("\r"); $expect->expect('Password:'); $expect->send($enpass); $expect->send("\r") ; } ], [ '#' ], ); $expect->interact();

i just keep getting timeout error (unable to connect to host: unable to establish master SSH connection: login timeout at).

any insight is greatly appreciated.

Replies are listed 'Best First'.
Re: EXPORT and Net::Openssh
by kcott (Chancellor) on Apr 18, 2013 at 21:43 UTC

    G'day th'timmay,

    Welcome to the monastery.

    Looking at the Net::OpenSSH Source, I see the timeout defaults to 90. You're setting it to 10. Perhaps increasing that value might help.

    Note the calculation in the _connect() method (which is called by new()):

    sub _connect { my ($self, $async) = @_; $self->_set_error; my $timeout = int((($self->{_timeout} || 90) + 2)/3); ...

    The default evalutes to 30; your value of 10 evalutes to 4 (or maybe even 3 if (10 + 2)/3) == 3.9999999).

    -- Ken

Re: EXPORT and Net::Openssh
by salva (Abbot) on Apr 19, 2013 at 07:29 UTC
    I can't see why your script behaves differently when you put the login parameters in a different module but in any case, instead of mixing them all in one string, you can pass then to Net::OpenSSH as different arguments:
    my $ssh = Net::OpenSSH->new($host, user => $user, password => $password, timeout => 10);
    You can also activate debugging in order to see what is going on:
    $Net::OpenSSH::debug = -1;
Re: EXPORT and Net::Openssh
by Illuminatus (Curate) on Apr 18, 2013 at 20:02 UTC
    It doesn't have anything to do with Net::OpenSSH, because the vars in question get expanded in the string before the 'new' call is made. Have you printed the expanded string $user:$pass\@$host under both conditions (locals and module)? That's what I'd do first.


Re: EXPORT and Net::Openssh
by th'timmay (Initiate) on Apr 19, 2013 at 13:10 UTC
    well i tried again when i came in this morning and voila! everything works just fine.

    thank you all for your inputs. i'm just going to chalk it up to the fact it's a vm running on a m$oft box and after the reboot everything is happy happy. :-)

    i've made a couple of mods to what i had and below is my working copy...
    #! /usr/bin/perl use strict; use warnings; use Net::OpenSSH; use Expect; use IO::Stty; use DateTime; use Creds qw($user $pass $enpass); my $logpath = '/var/log/myssh/'; my $host = $ARGV[0]; my $timeout = 5; my $connstr="$user:$pass\@$host"; my $dt = DateTime->now(time_zone=>'local'); my $log = $logpath.$host."-".$dt->datetime(); my $ssh = Net::OpenSSH->new("$connstr", timeout => 10); $ssh->error and die "unable to connect to host: ". $ssh->error; my ($pty, $pid) = $ssh->open2pty(); my $expect = Expect->init($pty); $expect->log_file($log); $expect->expect($timeout, [ '>' => sub { $expect->send('enable'); $expect->send("\r"); $expect->expect('Password:'); $expect->send($enpass); $expect->send("\r") ; $expect->expect('-re', '/\w+[#]\s/'); } ], [ '#' ], ); $expect->interact(); `gzip $log`;

    it's working well enough for authenticating to cisco devices, going to enable mode, and logging the session. admittedly it needs a bit more spit and polish, but first i'm gonna get more caffeine.

    happy coding!!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1029405]
Approved by salva
[holli]: i was just thinking of the bare content.
[LanX]: Plenty of "templates" realised with CGI-HTML-function
[LanX]: well if you want you can fetch all XML and translate it to another board markup
[moritz]: at this time, a migration could consist of only migrating the rendered HTML of nodes
[moritz]: to a forum software that supports raw HTML input for legacy threads, and uses sane markup for everything else
[holli]: yeah, then vroom will personally stab me in the back at night
[holli]: exactly, moritz
[moritz]: with a big table of redirects to support the old links
[LanX]: do it
[holli]: well if you put some cleverness into the migration you can auto convert most of the links as well

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2017-11-20 19:31 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (292 votes). Check out past polls.