Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: A little demo for Net::SSH2

by Always Improving (Initiate)
on Oct 15, 2010 at 18:27 UTC ( [id://865535]=note: print w/replies, xml ) Need Help??


in reply to Re: A little demo for Net::SSH2
in thread A little demo for Net::SSH2

I am using what I can glean from this thread to try to ssh into a cisco box and get a portion of it's config. What I am using below executes the two commands, sleeps 45 seconds, and by the time it gets past the sleep loop it has already closed the shell. Is there any way to make the shell stay open until I close it, if that makes sense? Like a keepalive of some kind?

if ($ssh) { eval { my $cs = Net::SSH2->new(); $cs->connect($_); my $ip = $_; my $done = false; open (OUTFILE, ">$ip.txt"); if ($cs->auth_keyboard($user,$pw)) # login { print "Opening SSH session to $_\n"; my $chan2 = $cs->channel(); $chan2->shell(); print $chan2 "term len 0\n"; print $chan2 "sh run | be line\n"; # until (<$chan2> =~ /line con 0/i) { # print "in the line con 0 loop"; print <$chan2>; # }; # prints everything above the "line con 0" line # print OUTFILE "line con 0\n"; # pri +nts the "line con 0" line # until ($done) { # print "do we even enter the until loop?\n\n"; # if (<$chan2> =~ /new/i) { # print "we are done!!!\n\n\n"; # $done = true; # } # print "the similarity of chan2 and end is " . (<$chan2> = +~ /new/i) . "\n"; # print <$chan2> . "here is another line " . $done; + # } # prints everything below the "line con 0" line my $count = 45; while ($count > 0) { print --$count; sleep 1; } until (<$chan2> =~ /line con 0/i) {1; }; print $chan2 "exit\n"; print "chan2 is empty, and we are awake\n"; close (OUTFILE); $chan2->close; # Log out of device print LOG "SSH to " . $ip . "\n"; } }; # End trying ssh

so, I fixed it, with

if ($ssh) { eval { my $cs = Net::SSH2->new(); $cs->connect($_); my $ip = $_; open (OUTFILE, ">$ip.txt"); if ($cs->auth_keyboard($user,$pw)) # login { print "Opening SSH session to $_\n"; my $chan2 = $cs->channel(); $chan2->shell(); print $chan2 "term len 0\n"; print $chan2 "sh run | be line\n"; print OUTFILE <$chan2>; print OUTFILE "line con 0\n"; + # prints the "line con 0" line print OUTFILE <$chan2>; + # prints everything below the "line con 0" line until (<$chan2> =~ /line con 0/i) {1;}; print OUTFILE <$chan2>; + # prints everything below the "line con 0" line close (OUTFILE); $chan2->close; # Log out of device print LOG "SSH to " . $ip . "\n"; } }; # End trying ssh

It must have been the blocking missing, and the untils were messing it up.

Replies are listed 'Best First'.
Re^3: A little demo for Net::SSH2
by zentara (Archbishop) on Oct 15, 2010 at 20:03 UTC
    It must have been the blocking missing,

    I didn't see you have a $chan->blocking(0/1), or if that is the type of blocking you are talking about.

    If you google for "Net::SSH2 timeout" you will find it has been tricky to get timeouts to work. The latest module has a few lines where timeouts can be set

    connect ( handle | host [, port [, Timeout => secs ]] )
    and
    poll ( timeout, arrayref of hashes ) #probably the timeout you want
    You might want to look at Net::SSH2 Command Timeout Before Completion for other modules that handle the timeouts better.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Hi, I want to run program in background in remote host. I tried this but does not work.
      #!/usr/bin/perl -w use strict; use warnings; use Net::SSH2; my $ssh2 = Net::SSH2->new(); $ssh2->connect("myhost.com") or die ("SSH2 Connect Error: $!"); $ssh2->auth_password($user, $pass) or die; my $chan1 = $ssh2->channel(); $chan1->blocking(0); $chan1->exec("./a.out &"); $chan1->close; $ssh2->disconnect();
      Any idea will be appreciated. Thanks.

        Explain "does not work". What do you expect, what happens instead? Any messages?

        Add error checks to your code.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://865535]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-19 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found