BernieC has asked for the wisdom of the Perl Monks concerning the following question:
and I tested it with:# NB: you can only do one command on a channel so we get the channel ## do the command give back the output then close the channel Note +that the command ## must be a fully "shell punctuated and escaped" sub docmd { my $chan = $ssh2->channel() or $ssh2->die_with_error ; $chan->exec("($_[0]) 2>&1") ; # my $out = ""; while (!$chan->eof) { my $buffer = ""; my $bytes = $chan->read($buffer, 100) ; $ssh2->die_with_error if $bytes < 0 ; $out .= $buffer ; } return $out ; }
print docmd("cd tmp; ls") ; print docmd("cd bin; ls") ; $ssh2->disconnect() ;
and I confess that I was amazed that it worked.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Running commands with Net::SSH2
by haukex (Archbishop) on Aug 10, 2018 at 17:41 UTC |