cmv has asked for the wisdom of the Perl Monks concerning the following question:
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Net::SSH::Perl - Breaking out of a running command?
by jellisii2 (Hermit) on Aug 06, 2014 at 11:43 UTC | |
by cmv (Chaplain) on Aug 06, 2014 at 15:25 UTC | |
|
METHOD1: Using break_client_loop() & undef($ssh)
by cmv (Chaplain) on Aug 06, 2014 at 15:23 UTC | |
|
Re: Net::SSH::Perl - Breaking out of a running command?
by Anonymous Monk on Aug 06, 2014 at 21:15 UTC | |
by cmv (Chaplain) on Aug 07, 2014 at 14:07 UTC | |
|
METHOD2: Using $chan->rcvd_ieof()
by cmv (Chaplain) on Aug 08, 2014 at 18:40 UTC | |
|
Re: Net::SSH::Perl - Creating multiple SSH2 Channels
by cmv (Chaplain) on Aug 07, 2014 at 14:21 UTC |