Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Net::SSH::Perl - Creating multiple SSH2 Channels

by cmv (Chaplain)
on Aug 07, 2014 at 14:21 UTC ( [id://1096638]=note: print w/replies, xml ) Need Help??


in reply to Net::SSH::Perl - Breaking out of a running command?

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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1096638]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 19:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found