http://qs321.pair.com?node_id=854068


in reply to A little demo for Net::SSH2

I found that some of the commands I was running over ssh (from Windows to an HP-UX system) were returning early from the read loop (and terminating the command), so I had to add extra code for executing and reading the results. This was part of a larger module, but the relevant method was:
sub cmd { my $self = shift; my $cmd = shift; my $ssh = $self->{ssh}; my $ch = $self->{ch}; unless ($ch) { $self->{ch} = $ch = $ssh->channel; $ch->ext_data('merge'); $ch->blocking(0); $ch->shell(); 1 while <$ch>; } print $ch "$cmd\n"; select(undef, undef, undef, 0.2); print $ch "print SSH2_cmd_done\n"; select(undef, undef, undef, 0.2); my @output; READ_LOOP: while (1) { local $_; while (<$ch>) { last READ_LOOP if /^SSH2_cmd_done/; push @output, $_; } sleep 1; } return @output; }

Replies are listed 'Best First'.
Re^2: A little demo for Net::SSH2
by Always Improving (Initiate) on Oct 15, 2010 at 18:27 UTC

    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.

      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.