Perl-Net::SSH2和缓冲

Perl-Net::SSH2和缓冲,perl,ssh,Perl,Ssh,有一个使用Net:SSH2模块的小perl脚本 #!/usr/bin/perl use strict; use warnings; use Net::SSH2; my $ssh2 = Net::SSH2->new(); $ssh2->connect('testhost') or die $!; if ($ssh2->auth_publickey('admin', 'id_rsa.pub', 'id_rsa')) { print "Connected\n"; } else {

有一个使用Net:SSH2模块的小perl脚本

#!/usr/bin/perl
use strict;
use warnings;
use Net::SSH2;

my $ssh2 = Net::SSH2->new();
$ssh2->connect('testhost') or die $!;
if ($ssh2->auth_publickey('admin', 'id_rsa.pub', 'id_rsa')) {
print "Connected\n";
} else {
print "failed";
}
my $sess = $ssh2->channel() or die "Unable to create chan\n";
$sess->blocking(1);
$sess->shell();
print $sess "sudo /root/remote.pl\n";
print "$_\n" while <$sess>;
$sess->send_eof;
$sess->close;

执行此操作后,有时我会看到/root/remote.pl的输出,有时则看不到。我相信问题在于输出缓冲,但我不知道如何解决这个问题。

如果在linux系统上使用此脚本,为什么不使用perl back tilt来使用linux命令而不是使用模块

例如:

#!/usr/bin/perl
use strict;
use warnings;

# Command
my $cmd = "ssh login:password@host; sudo /root/remote.pl; exit";

# Execute the command
`$cmd`

PS:如果remote.pl脚本有一些打印,您应该会看到它们。

实际上,我需要在远程主机上执行几个命令,并且我希望避免为每个命令创建ssh连接。实际上,您可以使用一些命令分隔命令;如果可能的话,很难正确使用Net::SSH2。改为使用Net::OpenSSH!听起来您在寻找ssh的-t optionNet::ssh::Perl的等价物,而use\u ptyNet::SSH2::Channel也有一个pty方法。