Folks-
As I'm learning about the guts of Net::SSH::Perl, I've discovered a neat way to generate multiple commands running on separate SSH2 channels (and how to pull in the output along with which channel it came from).
I figured it would be good to post this to help those who are trying to do something similar, and also to show progress while I'm trying to figure out how to delete a currently running channel (the main point of this thread).
Here is my latest code - please comment!
-Craig
UPDATE: Added listchannels()
use strict;
use warnings;
use Net::SSH::Perl;
my $HOST='MYHOST';
my $ID='MYLOGIN';
my $DEBUG=0;
my $PW='MYPW';
my $perlcmd = join('', <DATA>);
my $cmd = "/bin/perl - -heartbeat ";
# Login to host...
my $myssh = Net::SSH::Perl->new( $HOST, protocol=>2, debug=>$DEBUG );
$myssh->login($ID, $PW) or die "Failed to login: $!\n";
listchannels($myssh);
# Alarm Handler...
$SIG{ALRM} = sub {
print STDERR "\nALARM\n";
listchannels($myssh);
# Break out of currently running channel..
$myssh->break_client_loop; # Seems to work
};
# Display output of multiple commands running on multiple SSH2 channel
+s...
while(1) {
print STDERR "============================\n";
alarm 5;
remcount($myssh); # Add new channel
print STDERR "NEXT...\n";
}
sub listchannels
{
my $ssh = shift || die "Missing ssh object";
foreach my $x (@{$ssh->{channel_mgr}{channels}}) {
print STDERR "CHAN ", $x->{id}, " :$x\n";
}
}
sub remcount
{
my $ssh = shift || die "Missing ssh object";
# STDOUT handler (protocol 2 only)...
$ssh->register_handler('stdout', sub {
my ($channel, $buffer) = @_;
print STDERR "CHAN $channel->{id}: ", $buffer->bytes, "\n";
});
print STDERR "REMOTE: $cmd\n";
my ($stdout, $stderr, $exit) = $ssh->cmd($cmd, $perlcmd);
}
__DATA__
use strict;
use warnings;
$|++;
my $c='a';
while(1) {
print $c++;
sleep 1;
}