Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re^2: A little demo for Net::SSH2 by Always Improving
in thread A little demo for Net::SSH2 by zentara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2022-08-10 01:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?