我遇到Net :: SSH2 :: Channel的問題。我正在使用Oracle LINUX 2.6.39-400.214.5.el6uek.x86_64,使用perl-5.10.1-136.el6.x86_64。perl net的問題:: SSH2 ::通道
遠程服務器運行Debian 3.2.60-1 + deb7u3 x86_64。
perl代碼將被用作bash腳本中的函數。現在,這是一本簡單的perlscript。
我對編程perl不是很有經驗。到目前爲止,我只修改了現有的perlscript。
使用我的testscript,我可以使用SSH2登錄到遠程服務器。
然後,我能夠使用$chan->shell(<conmmand>);
執行一個且只有一個命令第二個命令似乎失敗,即使它將作爲腳本中的第一個shell命令成功執行。 - >雖然$chan->exec();
據說只支持通道上的一個命令,但$chan->shell();
應該可以在同一通道上多次使用。 用$chan-->close;
關閉頻道並重新打開頻道也無濟於事。 - >如何解釋$ssh2->error;
的輸出?
我的第二個問題是:究竟阻礙了什麼? 在ssh連接(ssh->blocking();
)和通道($chan->blocking(0);
)上使用阻塞之間有什麼區別?我見過兩個。 對我而言,在頻道上使用阻止或不使用頻道沒有任何效果。
最後,我不知道如何獲得執行命令的返回值以及如何以某種方式讀取它,以至於我不必事先知道它的長度。 (在testscript中使用的目錄/ tmpp不存在使用現有目錄不會改變任何內容 我的輸出始終與輸出的前65個字符相同uname -a
當我設置bufleng =70
時,腳本掛起。在這裏,事情似乎是完全錯誤的。
任何建議是極大的歡迎。 謝謝您的支持。
再見 拉爾夫
我的腳本如下:
#!/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";
輸出是:
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