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

In reply to Re: Net::SSH::Perl - Creating multiple SSH2 Channels by cmv
in thread Net::SSH::Perl - Breaking out of a running command? by cmv

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.