Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Net:SSH2 channels

by zentara (Archbishop)
on Aug 07, 2018 at 20:54 UTC ( [id://1220038]=note: print w/replies, xml ) Need Help??


in reply to Net:SSH2 channels

Hi, channel usage can be a bit tricky. See Net::SSH2 Interactive command example and Net::SSH2 just hangs up without any output The first thing to try the non-blocking mode on the channel.
$chan->blocking(1);
UPDATE:, as per salva's suggestion to use latest code. With the latest Net::SSH2 the following works for me on linux. Any use of $channel->blocking(0) immediately gives an error message, so $chan->blocking(1) is almost mandatory.
#!/usr/bin/perl use warnings; use strict; use Net::SSH2; # assuming a user named 'z' for demonstration # connecting to localhost, so you need your sshd running my $ssh2 = Net::SSH2->new(); $ssh2->connect('localhost') or die "Unable to connect Host $@ \n"; $ssh2->auth_password('z','ztester') or die "Unable to login $@ \n"; my $chan = $ssh2->channel(); $chan->blocking(1); $chan->exec('ls -la'); while (<$chan>){ print } # run a second command in $chan $chan->exec('dir'); while (<$chan>){ print } #will get dir named / thru another channel my $chan1 = $ssh2->channel(); $chan1->blocking(1); $chan1->exec('ls -la /'); while (<$chan1>){ print } #shell use my $chan2 = $ssh2->channel(); $chan2->blocking(1); $chan2->shell(); #opens shell on server print $chan2 "uname -a\n"; print "LINE1 : $_" while <$chan2>; # from perldoc Net::SSH2::Channel # Note that only one invocation of "process" or any of the shortcuts # "shell", "exec" or "subsystem" is allowed per channel. In order t +o run # several commands, shells or/and subsystems, a new "Channel" insta +nce # must be used for every one. # only 1 command per shell, the next line won't run print $chan2 "who\n"; print "LINE2 : $_" while <$chan2>; $chan2->close; $ssh2->disconnect(); exit; __END__

I'm not really a human, but I play one on earth. ..... an animated JAPH

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-26 03:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found