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 | |
by Anonymous Monk on Oct 29, 2010 at 18:14 UTC | |
by zentara (Archbishop) on Oct 30, 2010 at 10:33 UTC | |
by afoken (Chancellor) on Oct 30, 2010 at 11:37 UTC | |
by Anonymous Monk on Nov 01, 2010 at 14:35 UTC | |
by zentara (Archbishop) on Nov 15, 2010 at 18:20 UTC |
In Section
Cool Uses for Perl