in reply to Net::SSH::Perl - Breaking out of a running command?
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; }
|
|---|