Hi Monks-

I'm trying to run a remote command (via Net::SSH::Perl) that never returns, and would like to be able to break out of it whenever I want.

How can I do this?

Below is some sample code I've written to illustrate what I want to do.

In the alarm handler, I would like to break the currently running command (close doesn't work), then re-run the command to start counting again.

Any pointers are much appreciated!

Thanks

-Craig

Update: Searching through the code, I see that $ssh->_disconnect is available, but it is a null subroutine. Still looking...

Update 2: Using $ssh->break_client_loop seems to work. Not sure of side-effects yet...

use strict; use warnings; use Net::SSH::Perl; my $myssh; $SIG{ALRM} = sub { print STDERR "\nALARM\n"; $myssh->close; ### There is no close - what can I use? remcount($myssh); alarm 5; }; $myssh = Net::SSH::Perl->new( "MY.REMOTE.SERVER", protocol => 2 ); alarm 5; remcount($myssh); sub remcount { my $ssh = shift || die "missing ssh object"; $ssh->login("MYLOGIN", "MYPASSWORD") or die "Failed to login: $!\n +"; # STDOUT handler... $ssh->register_handler('stdout', sub { my ($channel, $buffer) = @_; print STDERR "OUT BYTES: ", $buffer->bytes, "\n"; }); my $perlcmd = join('', <DATA>); my $cmd = "/bin/perl - -heartbeat "; 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 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.