Folks-

I believe I've solved this issue (but still would like to have some more experienced folks comment on this solution).

Here is the code that seems to do what I want - critiques welcome!

Thanks

-Craig

Title Update: - this solution is brute-force. See additional posts in this thread for other techniques.

Update 2: - Adding $ssh->sock->close() to code. I like this solution because there seems to be no side effects (since you are taking down the entire ssh connection each time). Also, it should work with either SSH1 or SSH2 protocols.

I don't like this solution because it isn't very elegant or efficient (if you use SSH2, you should keep the ssh connection up and just use a new channel).

use strict; use warnings; use Net::SSH::Perl; my $ssh; $SIG{ALRM} = sub { print STDERR "\nALARM\n"; #$ssh->close; # There is no close - what can I use? #$ssh->_disconnect; # Null subroutine DOES NOT WORK $ssh->break_client_loop; # Seems to work $ssh->sock->close; }; my $perlcmd = join('', <DATA>); my $cmd = "/bin/perl - -heartbeat "; while(1) { alarm 5; remcount(); undef($ssh); print STDERR "NEXT...\n"; } sub remcount { $ssh = Net::SSH::Perl->new( "MYHOST", protocol=>2 ); $ssh->login("MYLOGIN", "MYPASS") or die "Failed to login: $!\n"; # STDOUT handler... $ssh->register_handler('stdout', sub { my ($channel, $buffer) = @_; print STDERR "OUT BYTES: ", $buffer->bytes, "\n"; }); print STDERR "REMOTE: $cmd\n"; my ($stdout, $stderr, $exit) = $ssh->cmd($cmd, $perlcmd); } __DATA__ use strict; use warnings; $|++; my $c=0; while(1) { print $c++; sleep 1; }

In reply to METHOD1: Using break_client_loop() & undef($ssh) by cmv
in thread Net::SSH::Perl - Breaking out of a running command? by cmv

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.