Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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.

creds.pm
#!/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 = '....';
myssh.pl
#! /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.

Comment on EXPORT and Net::Openssh
Select or Download Code
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.

    fnord

Re: EXPORT and Net::Openssh
by kcott (Abbot) 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 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1029405]
Approved by salva
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2014-12-29 14:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (188 votes), past polls