无法使用Net :: SSH2在远程服务器上执行后续命令

时间:2016-02-23 16:10:18

标签: perl ssh

我使用文本文件来保存一系列命令。使用Perl Net :: SSH2,我在远程服务器上执行它们。在第一个命令之后,我无法触发并获取命令输出。第一个是成功但是下一个命令在$ chan-> exec($ cmd)失败。

use warnings;
use strict;
use Net::SSH2;
my $host = hidden; my $usr = hidden;  my $pwd = hidden;
my $cmd_list; my $cmd;

my $session = Net::SSH2->new();
$session->connect($host) or die "Unable to connect Host $@ \n";
$session->auth_password($usr,$pwd) or die "Unable to login $@ \n";

my $chan = $session->channel();
open (cmd_list, 'sample.txt');
foreach $cmd (<cmd_list>) {
  $chan->exec($cmd) or print STDERR "ERR: Command execution failure\n";
  my $buflen = 10240;
  my $buf1 = '0' x $buflen;
  my $result = $chan->read($buf1, $buflen);
}
close (cmd_list); 

sample.txt的内容

hostname 
cd /home/tom 
ls -l

0 个答案:

没有答案