I'm having a Problem with Net::SSH2::Channel. I'm Working on Oracle LINUX 2.6.39-400.214.5.el6uek.x86_64, using perl-5.10.1-136.el6.x86_64.
The remote server runs Debian 3.2.60-1+deb7u3 x86_64.
The perl code is going to be used as a function inside a bash script. Right now, it is a plain perlscript.
I'm not very experienced to programmming perl. Until now, I only have adapred existing perlscripts.
With my testscript, I can log in to a remote server using SSH2.
Then, i am able to execute one - and only one - command using $chan->shell(<conmmand>);
The second command seems to fail, even if it would be executed successfully as 1st shell-command in the script.
--> While $chan->exec ();
is said to support only one command on the channel, $chan->shell();
should be useable more than once on the same channel.
Closing the channel with $chan-->close;
and reopening it again does not help,either.
--> How do I interpret the output of $ssh2->error;
?
My second question is: what exactly is blocking doing?
And what is the difference between using blocking on the ssh connection (ssh->blocking();
) and on the channel ($chan->blocking(0);
)? I have seen both.
For me, there was no effect using blocking on the channel or not using it.
Finally, I don`t know, how to get the returnvalue of an executed command and how to read it in a way, that I dont have to know its length in advance.
(The directory /tmpp, which is used in the testscript, does not exist. Using an existing directory does not change anything.
My output is always the identical to the first 65 characters of the output of uname -a
When I set bufleng =70
, the script hangs. Here, something seems to be completely wrong.
Any suggestions are greatly welcome. Thank you for your support.
Bye Ralf
my script is as follows:
#!/usr/bin/perl
use warnings;
use strict;
use Net::SSH2;
use Data::Dumper;
# Handling options
my $host = shift;
my $user = shift;
my $passwd = shift;
my $command = shift;
my $destination = shift;
#my $source = $ARGV;
# Defining variables
my $LEC="";
#my $scp_session=new Net::ssh2 ( Errmode=>'return',
my $ssh2=Net::SSH2 -> new();
$ssh2->debug(1);
##print Dumper($ENV);
### Connecting (10)
#die "can\'t connect to ${host}, Error 10" unless $ssh2->connect($host);
#print "Connected to ${host}\n";
$ssh2->connect($host);
if ("$?"==0) {
print "Connected to ${host}\n";
} else {
print "can\'t connect to ${host}, Error 10\n";
#print $ssh2->error;
print "\n";
}
### Logging in (20)
#die "can't authenticate as ${user}" unless
#$ssh2->auth(username => '${user}',password => '${passwd}');
$ssh2->auth_password(${user},${passwd});
# print $ssh2->error;
# #print "\n";
if ("$?"==0) {
print " Authenticated as ${user}\n";
} else {
print $ssh2->error;
print " Authentication failed\n";
exit 20
}
print " ## step 0\n";
### workload (30)
my $chan = $ssh2->channel();
print $ssh2->error;
print "\n ## step 2\n";
#$chan->blocking(0);
# print $ssh2->error;
# print "\n ## step 3\n";
#$chan->shell('set cli-parameters console pager disabled');
# print $ssh2->error;
# print "\n ## step 4\n";
# $chan->close;
# my $chan = $ssh2->channel();
### testing
$chan->shell('ls -la');
print $ssh2->error;
print "\n ## step 5\n";
my $buflen = 65;
my $buf1 = '0' x $buflen;
$chan->read($buf1, $buflen);
print $buf1,"\n";
$chan1->shell(${command});
print $ssh2->error;
print "\n ## step 6\n";
#my $buflen = 70;
print $ssh2->error;
print "\n ## step 7\n";
my $buf2 = '0' x $buflen;
print $ssh2->error;
print "\n ## step 8\n";
$chan->read($LEC, $buflen);
print $ssh2->error;
print "\n ## step 9\n";
print "${command}:\n", $buf1,"\n";
print $ssh2->error;
print "\n ## step 10\n";
$chan->shell('exit');
print $ssh2->error;
print "\n ## step 11\n";
### Logging off and disconnecting (90)
$ssh2->disconnect($host);
#print $ssh2->error;
#print "\n";
#($ssh2->auth_password($user,$passwd)) {
#print "\n Executing command...\n";
my $cmd = "ls";
#print " ==> Running $cmd\n";
The output is:
perl ./test2.perl ifbscdd root dcdiag "LEC=1 ; [ -d /tmp ] && LEC=9 ; export LEC ; return $LEC"
Connected to ifbscdd
Authenticated as root
## step 0
libssh2_channel_open_ex(ss->session, pv_channel_type, len_channel_type, window_size, packet_size, ((void *)0) , 0 ) -> 0x1b89fc0
0
## step 2
0
## step 5
Net::SSH2::Channel::read(size = 65, ext = 0)
- read 65 bytes
- read 65 total
Linux ifbscdd 3.2.0-4-amd64 #1 SMP Debian 3.2.60-1+deb7u3 x86_64
libssh2_channel_open_ex(ss->session, pv_channel_type, len_channel_type, window_size, packet_size, ((void *)0) , 0 ) -> 0x1ba6490
0
## step 6
0
## step 7
0
## step 8
Net::SSH2::Channel::read(size = 65, ext = 0)
- read 0 bytes
- read 0 total
0
## step 9
LEC=1 ; [ -d /tmp ] && LEC=9 ; export LEC ; return :
Linux ifbscdd 3.2.0-4-amd64 #1 SMP Debian 3.2.60-1+deb7u3 x86_64
0
## step 10
-1LIBSSH2_ERROR_SOCKET_NONEFailed waiting for channel success
## step 11
Net::SSH2::Channel::DESTROY
Net::SSH2::Channel::DESTROY
Net::SSH2::DESTROY object 0x1aa6230