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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |